在android中以n个间隔对recyclerview项目的TextView进行多种背景颜色

Abh*_*Jha 2 android android-layout android-recyclerview

我需要让我的RecyclerView项目的TextView显示多种背景颜色.假设我有7个不同的颜色代码,我需要在每7个项目后显示.这是我的方法.请帮忙!!

@Override
public void onBindViewHolder(BuyCategoriesViewHolder holder, final int position) {
    holder.tv_name.setText(category.get(position).getCategory());
    char firstChar=category.get(position).getCategory().charAt(0);
    holder.tv_title.setText(String.valueOf(firstChar));

    if(position == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
    }else if(position %7 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
    }else if(position %6 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
    }else if(position %5 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
    }else if(position %4 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
    }else if(position %3 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
    }else if(position %2 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
    }else {
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
    }

    //holder.tv_title.setBackgroundResource(R.drawable.shape_circle);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callItemViewListener.callItemView(position);
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

Rob*_*bCo 8

您使用模数运算符错误.试试这样:

        if (position % 7 == 0) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
        } else if(position % 7 == 1) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
        } else if(position % 7 == 2) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
        } else if(position % 7 == 3) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
        } else if(position % 7 == 4) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
        } else if(position % 7 == 5) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
        } else if(position % 7 == 6) {
            holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
        }
Run Code Online (Sandbox Code Playgroud)

或者使用switch语句:

        int colorRes = 0;
        switch(position % 7) {
            case 0: colorRes = R.color.list_color1;
                break;
            case 1: colorRes = R.color.list_color2;
                break;
            case 2: colorRes = R.color.list_color3;
                break;
            case 3: colorRes = R.color.list_color4;
                break;
            case 4: colorRes = R.color.list_color5;
                break;
            case 5: colorRes = R.color.list_color6;
                break;
            case 6: colorRes = R.color.list_color7;
                break;
        }
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext, colorRes));
Run Code Online (Sandbox Code Playgroud)

编辑
为了完整性,请将@iClaude的答案与示例结合起来:

// define colors in the adapter  
private final int[] backgroundColors = {R.color.list_color1, R.color.list_color2, R.color.list_color3,
        R.color.list_color4, R.color.list_color5, R.color.list_color6, R.color.list_color7};

    // in onBindViewHolder
    int bgColor = ContextCompat.getColor(mContext, backgroundColors[position % 7]);
    holder.tv_title.setBackgroundColor(bgColor);
Run Code Online (Sandbox Code Playgroud)