Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在RecyclerViews的GridLayoutManager中为每个项目设置不同的颜色_Android_Android Recyclerview_Realmrecyclerviewadapter - Fatal编程技术网

Android 如何在RecyclerViews的GridLayoutManager中为每个项目设置不同的颜色

Android 如何在RecyclerViews的GridLayoutManager中为每个项目设置不同的颜色,android,android-recyclerview,realmrecyclerviewadapter,Android,Android Recyclerview,Realmrecyclerviewadapter,我已经实现了GridLayoutManager,它运行良好。我使用了CardView作为项目布局 GridLayoutManager的屏幕截图: 我只想为列表中的前五项设置不同的颜色,然后在接下来的五项中重复相同的颜色。例如,如果我的列表包含10个项目,那么前五个项目具有不同的颜色假设红色、绿色、蓝色、黄色、粉色,然后从项目6到10,这些相同的颜色应设置为背景色。我尝试使用setCardBackgroundColorColor.parseColorFF6363设置CardView背景色。但这只会

我已经实现了GridLayoutManager,它运行良好。我使用了CardView作为项目布局

GridLayoutManager的屏幕截图:

我只想为列表中的前五项设置不同的颜色,然后在接下来的五项中重复相同的颜色。例如,如果我的列表包含10个项目,那么前五个项目具有不同的颜色假设红色、绿色、蓝色、黄色、粉色,然后从项目6到10,这些相同的颜色应设置为背景色。我尝试使用setCardBackgroundColorColor.parseColorFF6363设置CardView背景色。但这只会更改所有项目的背景色。有没有办法为物品设置不同的颜色

@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);
}

您必须在onBindViewHolder中更改它

我认为这段代码可能适合你,只要使用开关,你的位置的模数加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"));
    }
}

要使用运行时生成的随机颜色执行此操作,您可以这样做

@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));
}
更新


这将为视图设置随机颜色,并且当您滚动内容时,颜色不会改变,因为onCreateViewHolder为每个视图调用一次,而onBindViewHolder在任何内容滚动时调用多次。

我使用了随机方法来选择颜色。但每当我向下滚动并返回顶部时,颜色都不同,这会产生糟糕的用户体验。所以你想要随机颜色,但你不想改变它们?我最初的想法是随机设置颜色。但是,系统可能会将背景颜色分配给recyclerView中的一张卡。这可能会使卡片从列表中丢失,因为其他卡片可能有不同的颜色。
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);
}