我正在努力做到这一点
虽然我有网格视图中的项目,但我想要像图片中那样的项目的大小/颜色。
这是我的代码
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageView;
if (convertView == null) {
imageView = new ImageButton(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageButton) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setBackgroundColor(mThumbIds[0].);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.town_hall1,
R.drawable.town_hall2,
R.drawable.town_hall3,
R.drawable.town_hall4,
R.drawable.town_hall5,
R.drawable.town_hall6,
R.drawable.town_hall7,
R.drawable.town_hall8,
R.drawable.town_hall9,
R.drawable.town_hall10,
R.drawable.town_hall11,
};
}
我是初学者,请亲自为我提供构建该布局的步骤。 谢谢
答案 0 :(得分:3)
如果您在mThumbIds
中有假数据,那么您可以在getView()
中使用切换案例,如下所示:
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageView;
if (convertView == null) {
switch(position){
case 1:
imageView = new ImageButton(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 10);
imageView.setImageResource(mThumbIds[1]);
imageView.setBackgroundColor(mThumbIds[0].);
break;
case 2:
imageView = new ImageButton(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(mThumbIds[2]);
imageView.setBackgroundColor(mThumbIds[0].);
break;
case 3:
imageView = new ImageButton(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5)
imageView.setImageResource(mThumbIds[3]);
imageView.setBackgroundColor(mThumbIds[0].);
break;
}
}
else
{
imageView = (ImageButton) convertView;
}
return imageView;
}
您可以将案件数量等于图片数量。您已为不同的图像设置了不同的属性。
答案 1 :(得分:2)
要使ID Col1 Counter
1 X 1
2 X 2
3 A 2
4 B 2
5 X 3
6 C 3
7 X 4
8 D 4
看起来像上面附带的示例,您必须创建扩展GridView
的类。
覆盖RecyclerView.ItemDecoration
可随意使用边缘和空格。
getItemOffsets()
然后只需在 public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
Log.d("position: : ", String.valueOf(Float.valueOf(position % spanCount)));
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
Log.d("spacing: ", String.valueOf(spacing));
Log.d("spacingL: ", String.valueOf(outRect.left));
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
上调用addItemDecoration()
方法,将RecyclerView
类的新实例作为参数传递。
答案 2 :(得分:1)
如果您想为视图获得不同的背景颜色,可以尝试以下方法:
public int getRandomColor(int position) {
int[] colorArray = {Color.RED, Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY};
if (position > colorArray.length)
position %= colorArray.length;
return colorArray[position];
}
将其用作:
imageView.setBackgroundColor(getRandomColor(position));
PS:没有任何编辑器,所以它可能包含拼写错误。