在recycleview onclick项目中如何更改其他项目的颜色 [英] In recycleview onclick item how to change the color of other item
问题描述
我正在尝试创建测验应用程序.单击在回收视图中的一个项目时,无法弄清楚如何更改其他项目的颜色.单击选项2,但正确的选项是选项1时,则应如下图所示显示.请解决
I am trying to create a quiz app. Could not figure out how to change the color of other item when one item in recycleview is clicked.When option 2 is clicked but the correct option is option 1 it should display as given below in picture. Solution please
推荐答案
为可绘制文件夹中的选定按钮和未选定按钮创建两个可绘制文件.
Create two drawable files for selected and unselected buttons in your drawable folder.
创建如下的模型类:
public class ModelDemo
{
//your declaration
boolean isClicked;
public void setIsClicked(boolean value) {
this.value = value ;
}
public boolean isClicked() {
return value;
}
}
///现在创建类型模型的数组列表,例如ArrayList<ModelDemo>
,并使用其他值来添加您的recyclerview.最初将isClicked设置为false.如下添加您的recylerview
//Now create an arraylist of type model like ArrayList<ModelDemo>
, with other values you need to infalte your recyclerview. Set the isClicked to false initially. Inflate your recylerview as follows
public void onBindViewHolder(final Holder holder, final int position) {
final ModelDemo modelDemo= arrayZipModel.get(position);
//here write your code to inflate the data for button text
if (modelDemo.isClicked())
holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.selected));
else
holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.unselected));
holder.yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(modelDemo.isClicked())
modelDemo.setIsClicked(false)
else
modelDemo.setIsClicked(true)
notifyDataSetChanged();
}
});
}
这篇关于在recycleview onclick项目中如何更改其他项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!