【问题标题】:How to set different color for each items in RecyclerViews' GridLayoutManager如何为 RecyclerView 的 GridLayoutManager 中的每个项目设置不同的颜色
【发布时间】:2021-01-23 22:11:13
【问题描述】:

我已经实现了 GridLayoutManager,它工作正常。我使用 CardView 进行项目布局。

GridLayoutManager 的屏幕截图:

我只想为列表中的前五个项目设置不同的颜色,并为接下来的五个项目重复相同的颜色。例如,如果我的列表包含 10 个项目,那么前五个项目具有不同的颜色,假设红色、绿色、蓝色、黄色、粉红色,然后从项目 6 到 10,这些相同的颜色应该设置为背景颜色。我尝试使用 setCardBackgroundColor(Color.parseColor("#FF6363")) 设置 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);
}

【问题讨论】:

    标签: android android-recyclerview realmrecyclerviewadapter


    【解决方案1】:

    你必须在你的 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"));
        }
    }
    

    【讨论】:

      【解决方案2】:

      要使用在运行时生成的随机颜色,您可以这样做。

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

      更新

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

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

      【讨论】:

      • 我使用随机方法来选择颜色。但是每当我向下滚动并返回顶部时,颜色就会不同,这会导致用户体验不佳。
      • 所以你想要随机颜色,但又不想改变它们?
      • 我最初的想法是随机设置颜色。但是,系统有可能将背景颜色分配给 recyclerView 中的一张卡片。这可能会使卡片从列表中丢失,因为其他卡片可能有不同的颜色。
      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多