更改 RecyclerView 中所有卡片的文本颜色
Change text's color for all cards in RecyclerView
我必须做以下事情:当用户单击按钮时,我需要更改 RecyclerView 中所有卡片的标题颜色。我一直在搜索和搜索,但找不到可以帮助我的东西,除了这个方法:findViewHolderForAdapterPosition(int i)。不幸的是,它 returns null ().
您可以在您的适配器中声明一个新的布尔变量,我们称它为 changeColor
在适配器构造函数中将其初始化为 false
,然后在您的 onClick
操作方法中将其设置为true
并调用 yourAdapter.notifyDataSetChanged()
。最后将测试添加到适配器的 onBindViewHolder
方法中,类似于:
// your onclick method
void onClick() {
//code..;
changeColor = true;
yourAdapter.notifyDataSetChanged();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(changeColor)
holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, newTitleColor));
else
holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, defaultTitleColor));
}
我必须做以下事情:当用户单击按钮时,我需要更改 RecyclerView 中所有卡片的标题颜色。我一直在搜索和搜索,但找不到可以帮助我的东西,除了这个方法:findViewHolderForAdapterPosition(int i)。不幸的是,它 returns null (
您可以在您的适配器中声明一个新的布尔变量,我们称它为 changeColor
在适配器构造函数中将其初始化为 false
,然后在您的 onClick
操作方法中将其设置为true
并调用 yourAdapter.notifyDataSetChanged()
。最后将测试添加到适配器的 onBindViewHolder
方法中,类似于:
// your onclick method
void onClick() {
//code..;
changeColor = true;
yourAdapter.notifyDataSetChanged();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(changeColor)
holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, newTitleColor));
else
holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, defaultTitleColor));
}