[英]How to set different color for each items in RecyclerViews' GridLayoutManager
I've implemented the GridLayoutManager and it is working fine.我已经实现了 GridLayoutManager 并且工作正常。 I've used a CardView for the item layout.我使用 CardView 进行项目布局。
Screenshot of the GridLayoutManager: GridLayoutManager 的截图:
I just want to set a different color for the first five items in the list and repeat the same colors for the next five.我只想为列表中的前五个项目设置不同的颜色,并为接下来的五个项目重复相同的 colors。 For example, If my list contains 10 items, then the first five items have different colors suppose red, green, blue, yellow, pink and then from the item 6 - 10 these same colors should be set as the background color.例如,如果我的列表包含 10 项,那么前 5 项具有不同的 colors 假设红色、绿色、蓝色、黄色、粉红色,然后从第 6 到 10 项中,这些相同的 colors 应设置为背景颜色。 I've tried to set the CardView background color using setCardBackgroundColor(Color.parseColor("#FF6363")).But this simply changes the background color of all the items.我尝试使用 setCardBackgroundColor(Color.parseColor("#FF6363")) 设置 CardView 背景颜色。但这只是改变了所有项目的背景颜色。 Is there any way to set different colors to items?有没有办法为项目设置不同的 colors ?
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.device_card, parent, false);
CardView dc = view.findViewById(R.id.cardViewCard);
dc.setCardBackgroundColor(Color.parseColor("#FF6363"));
return new ViewHolder(view);
}
You got to change it in your onBindViewHolder.你必须在你的 onBindViewHolder 中改变它。
I think this code may work for you, just use the switch with the modulus of your position plus 1 divided by 5, and then In every case, you set the color you want as background.我认为这段代码可能对你有用,只需使用 position 的模数加 1 除以 5 的开关,然后在每种情况下,将所需的颜色设置为背景。
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
switch((position+1)%5) {
case 0:
dc.setCardBackgroundColor(Color.parseColor("#Color5"));
break;
case 1:
dc.setCardBackgroundColor(Color.parseColor("#Color1"));
break;
case 2:
dc.setCardBackgroundColor(Color.parseColor("#Color2"));
break;
case 3:
dc.setCardBackgroundColor(Color.parseColor("#Color3"));
break;
case 4:
dc.setCardBackgroundColor(Color.parseColor("#Color4"));
}
}
To to do it with a random color, generated at runtime you can do it this way.要使用在运行时生成的随机颜色,您可以这样做。
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
Random rand = new Random();
Int red = rand.nextInt(255 - 1)
Int green = rand.nextInt(255 - 1)
Int blue = rand.nextInt(255 - 1)
dc.setCardBackgroundColor(Color.argb(1,red,green,blue));
}
UPDATE更新
Random rand = new Random(); //declare this in adapter class, instead of creating multiple times do it once
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.device_card, parent, false);
CardView dc = view.findViewById(R.id.cardViewCard);
Int red = rand.nextInt(255 - 1)
Int green = rand.nextInt(255 - 1)
Int blue = rand.nextInt(255 - 1)
dc.setCardBackgroundColor(Color.argb(1,red,green,blue));
return new ViewHolder(view);
}
This will set random colors to the view and the colors won't change when you scroll the content, Since the onCreateViewHolder
called once for each view, whereas the onBindViewHolder
called many times when ever content scrolls .这会将随机 colors 设置为视图,并且当您滚动内容时 colors 不会改变,因为onCreateViewHolder
为每个视图调用一次,而onBindViewHolder
在内容滚动时调用多次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.