如何制作具有不同 Item 大小的 Listview 或 Gridview?
How to make Listview or Gridview with different Item size?
我想要 GridView 或 ListView,其中第一行的一个项目的宽度较大,第二个项目的宽度较小但高度相同。在那之后,对于第二行,一个项目的宽度很小,第二个项目的宽度很大。这个模式一直持续到列表的末尾。
就像下面给出的那样:
如果您的选择不限于列表视图和网格视图,请试试这个
StaggeredGrid
如果您愿意,您也可以使用 recyclerView,它包含一些逻辑。
我没有在库中搜索列表,但我根据布局制定逻辑并解决了列表问题。
在这里,我想分享我的代码,我根据给定的问题得到了列出的要求。
我根据给定 listing.Now 给定的权重,在每两个图像中使用两个线性布局进行布局我把逻辑奇数和偶数。奇数第一个 LinearLayout 可见,偶数第二个 LinearLayout 可见。
下面是我为列表行制作的布局。
<LinearLayout
android:id="@+id/lvFirst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp">
<ImageView
android:id="@+id/imgOne"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="2.0"
android:padding="3dp"/>
<ImageView
android:id="@+id/imgTwo"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="3dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lvSecond"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp">
<ImageView
android:id="@+id/imgThree"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="3dp"/>
<ImageView
android:id="@+id/imgFour"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="2.0"
android:padding="3dp"/>
</LinearLayout>
列表视图的适配器代码如下:
public class ImageAdapterNew extends BaseAdapter {
private Context mContext;
private int[] mThumbIds;
boolean mIsPro;
ArrayList<HashMap<String, Integer>> imgList = new ArrayList<>();
public ImageAdapterNew(Context c, int numPics,
String imgName, boolean isPro) {
mContext = c;
mThumbIds = new int[numPics];
int count = 0;
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < numPics; i++) {
mThumbIds[i] = c.getResources().getIdentifier("small_" +
imgName + "_" + i, "drawable", c.getPackageName());
if (count >= 2) {
count = 0;
imgList.add(map);
map = new HashMap<>();
map.put(count + "", mThumbIds[i]);
} else {
map.put(count + "", mThumbIds[i]);
}
count++;
}
mIsPro = isPro;
}
public int getCount() {
return imgList.size();
}
public Object getItem(int position) {
return imgList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.image_grid_new, parent, false);
holder = new ViewHolder();
holder.lvFirst = (LinearLayout) row.findViewById(R.id.lvFirst);
holder.lvSecond = (LinearLayout) row.findViewById(R.id.lvSecond);
holder.imgOne = (ImageView) row.findViewById(R.id.imgOne);
holder.imgTwo = (ImageView) row.findViewById(R.id.imgTwo);
holder.imgThree = (ImageView) row.findViewById(R.id.imgThree);
holder.imgFour = (ImageView) row.findViewById(R.id.imgFour);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
if (position % 2 == 0) {
holder.lvFirst.setVisibility(View.VISIBLE);
holder.lvSecond.setVisibility(View.GONE);
if (mThumbIds[position] != 0) {
Picasso.with(mContext).load(imgList.get(position).get("0")).noFade().resize(300, 533).centerInside().into(holder.imgOne); Picasso.with(mContext).load(imgList.get(position).get("1")).noFade().resize(300, 533).centerInside().into(holder.imgTwo);
//If you don't want to use Picasso comment previous line and uncomment next line
//imageView.setImageResource(mThumbIds[position]);
}
} else {
holder.lvFirst.setVisibility(View.GONE);
holder.lvSecond.setVisibility(View.VISIBLE);
if (mThumbIds[position] != 0) {
Picasso.with(mContext).load(imgList.get(position).get("0")).noFade().resize(300, 533).centerInside().into(holder.imgThree); Picasso.with(mContext).load(imgList.get(position).get("1")).noFade().resize(300, 533).centerInside().into(holder.imgFour);
//If you don't want to use Picasso comment previous line and uncomment next line
//imageView.setImageResource(mThumbIds[position]);
}
}
return row;
}
static class ViewHolder {
ImageView imgOne, imgTwo, imgThree, imgFour;
LinearLayout lvFirst, lvSecond;
}
}
欢迎提出任何建议或意见来改进代码。
我想要 GridView 或 ListView,其中第一行的一个项目的宽度较大,第二个项目的宽度较小但高度相同。在那之后,对于第二行,一个项目的宽度很小,第二个项目的宽度很大。这个模式一直持续到列表的末尾。
就像下面给出的那样:
如果您的选择不限于列表视图和网格视图,请试试这个 StaggeredGrid
如果您愿意,您也可以使用 recyclerView,它包含一些逻辑。
我没有在库中搜索列表,但我根据布局制定逻辑并解决了列表问题。 在这里,我想分享我的代码,我根据给定的问题得到了列出的要求。 我根据给定 listing.Now 给定的权重,在每两个图像中使用两个线性布局进行布局我把逻辑奇数和偶数。奇数第一个 LinearLayout 可见,偶数第二个 LinearLayout 可见。
下面是我为列表行制作的布局。
<LinearLayout
android:id="@+id/lvFirst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp">
<ImageView
android:id="@+id/imgOne"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="2.0"
android:padding="3dp"/>
<ImageView
android:id="@+id/imgTwo"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="3dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lvSecond"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp">
<ImageView
android:id="@+id/imgThree"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="3dp"/>
<ImageView
android:id="@+id/imgFour"
android:scaleType="centerCrop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="2.0"
android:padding="3dp"/>
</LinearLayout>
列表视图的适配器代码如下:
public class ImageAdapterNew extends BaseAdapter {
private Context mContext;
private int[] mThumbIds;
boolean mIsPro;
ArrayList<HashMap<String, Integer>> imgList = new ArrayList<>();
public ImageAdapterNew(Context c, int numPics,
String imgName, boolean isPro) {
mContext = c;
mThumbIds = new int[numPics];
int count = 0;
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < numPics; i++) {
mThumbIds[i] = c.getResources().getIdentifier("small_" +
imgName + "_" + i, "drawable", c.getPackageName());
if (count >= 2) {
count = 0;
imgList.add(map);
map = new HashMap<>();
map.put(count + "", mThumbIds[i]);
} else {
map.put(count + "", mThumbIds[i]);
}
count++;
}
mIsPro = isPro;
}
public int getCount() {
return imgList.size();
}
public Object getItem(int position) {
return imgList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.image_grid_new, parent, false);
holder = new ViewHolder();
holder.lvFirst = (LinearLayout) row.findViewById(R.id.lvFirst);
holder.lvSecond = (LinearLayout) row.findViewById(R.id.lvSecond);
holder.imgOne = (ImageView) row.findViewById(R.id.imgOne);
holder.imgTwo = (ImageView) row.findViewById(R.id.imgTwo);
holder.imgThree = (ImageView) row.findViewById(R.id.imgThree);
holder.imgFour = (ImageView) row.findViewById(R.id.imgFour);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
if (position % 2 == 0) {
holder.lvFirst.setVisibility(View.VISIBLE);
holder.lvSecond.setVisibility(View.GONE);
if (mThumbIds[position] != 0) {
Picasso.with(mContext).load(imgList.get(position).get("0")).noFade().resize(300, 533).centerInside().into(holder.imgOne); Picasso.with(mContext).load(imgList.get(position).get("1")).noFade().resize(300, 533).centerInside().into(holder.imgTwo);
//If you don't want to use Picasso comment previous line and uncomment next line
//imageView.setImageResource(mThumbIds[position]);
}
} else {
holder.lvFirst.setVisibility(View.GONE);
holder.lvSecond.setVisibility(View.VISIBLE);
if (mThumbIds[position] != 0) {
Picasso.with(mContext).load(imgList.get(position).get("0")).noFade().resize(300, 533).centerInside().into(holder.imgThree); Picasso.with(mContext).load(imgList.get(position).get("1")).noFade().resize(300, 533).centerInside().into(holder.imgFour);
//If you don't want to use Picasso comment previous line and uncomment next line
//imageView.setImageResource(mThumbIds[position]);
}
}
return row;
}
static class ViewHolder {
ImageView imgOne, imgTwo, imgThree, imgFour;
LinearLayout lvFirst, lvSecond;
}
}
欢迎提出任何建议或意见来改进代码。