gpt4 book ai didi

android - 反复设置recyclerview项目背景色

转载 作者:行者123 更新时间:2023-11-29 14:59:27 25 4
gpt4 key购买 nike

我试图在 cardview 中设置不同的背景颜色,但它不起作用我有包含颜色的颜色数组

public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.MyViewHolder> {
private Context mContext;
private List<String> leavetypeList;
private List<String> leavebalanceList;

public String[] mColors = {
"3F51B5","FF9800","009688","673AB7"
};


public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView leavecount,leavename;
public ImageView thumbnail;
public RelativeLayout rlauthor;
CardView cardView;
View viewline;

public MyViewHolder(View view) {
super(view);
leavecount = (TextView)view.findViewById(R.id.tvleavenumber);
leavename = (TextView)view.findViewById(R.id.tvleavetype);
cardView=(CardView) view.findViewById(R.id.cdvdashboard);
}
}

public DashboardAdapter(Context context, List<String> leavetypeList,List<String> leavebalanceList) {
this.mContext = context;
this.leavetypeList = leavetypeList;
this.leavebalanceList = leavebalanceList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.raw_dashboard, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Typeface tf;
tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/HKNova-Medium.ttf");
holder.leavecount.setTypeface(tf);
holder.leavename.setTypeface(tf);

holder.leavecount.setText(leavebalanceList.get(position));
holder.leavename.setText(leavetypeList.get(position));

String color="#"+mColors[position];

for(int c=1;c<mColors.length;c++)
{

holder.cardView.setCardBackgroundColor(Color.parseColor(color));

if(c==mColors.length)
{
c=1;
}
}


}

@Override
public int getItemCount() {
return leavetypeList.size();
}
}

我想要这种类型的输出,但只得到最后一种颜色,如果颜色完成而不是显示其他项目背景颜色从颜色 [0] 位置开始

非常感谢任何帮助

enter image description here

最佳答案

它不会那样工作。您需要将此逻辑放在 onBindViewHolder 中,您可以在其中借助 position 更改颜色。像这样使用

public String[] mColors = {"#3F51B5","#FF9800","#009688","#673AB7"};

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.itemView.setBackgroundColor(Color.parseColor(mColors[position % 4])); // 4 can be replaced by mColors.length
}

看看我是如何用 # 前缀重新定义 mColors 数组的。所以需要在计算颜色的时候加上#。

关于android - 反复设置recyclerview项目背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com