如何在RecyclerView中更改分配给ImageButton的矢量的颜色? [英] How to change the color of vector assigned to ImageButton in a RecyclerView?
问题描述
我有一个recyclerview,其中显示了几张(8)卡视图.在每个卡片视图内,有多个元素,例如文本,图像按钮等.在Adapter类内部(RvAdapter扩展了RecyclerView.Adapter),我有如下代码.我要做的是使用同一按钮的OnClickListener更改分配给imagebutton的Vector(.xml)的颜色.
I have a recyclerview which shows several(8) cardviews. Inside each cardview, there are several elements like text, imagebutton,etc. Inside the Adapter class (RvAdapter extends RecyclerView.Adapter) I have a code as below. What I want to do is change the color of Vector (.xml) assigned to the imagebutton using the OnClickListener of the same button.
public RvAdapter(Context context,List<Person> persons) {
this.mContext=context;
this.persons=persons;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_content,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ViewHolder mHolder=holder;
mHolder.personName.setText(persons.get(position).name);
mHolder.personAge.setText(( persons.get(position).age));
mHolder.cvCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"card number:"+String.valueOf(position),Toast.LENGTH_SHORT).show();
}
});
//this is where the error comes
mHolder.alarmImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//this code line doesnt work
//mHolder.alarmImageButton.setColorFilter(R.color.colorAccent);
//this code line works... but it changes the imagebutton color of another card view at below of the recycler view
mHolder.alarmImageButton.setColorFilter(Color.argb(255,255,255,255));
Toast.makeText(mContext,"button:"+String.valueOf(position),Toast.LENGTH_SHORT).show();
}
});
}
RecyclerView里面有8张卡片视图.单击第一个卡片视图的图像按钮的图像按钮后,它将颜色更改为白色.但是错误是它也改变了第七张卡的图像按钮的颜色.单击第二张卡片视图的图像按钮时,它也会更改第八张卡片的图像按钮颜色.但是,Toast可以正常工作.它表明单击事件已正确识别每个卡片视图和图像按钮的位置.
RecyclerView has 8 cardviews inside it. When the imagebutton of the first cardview's imagebutton is clicked, it changes the color to white. But the error is it changes the color of 7th card's imagebutton color too. When the second cardview's imagebutton is clicked it changes the 8th card's imagebutton color too. However, the Toast works fine. It shows that click event has identify the position of each cardview and imagebutton properly.
我只是无法正确解决这个问题.第一卡第七卡视图如何相互连接?
I just couldn't figure this out properly. How the 1st card 7th card views are connected with each other ?
这是我的ViewHolder类
This is my ViewHolder class
public class ViewHolder extends RecyclerView.ViewHolder{
CardView cvCard;
TextView personName;
TextView personAge;
ImageButton alarmImageButton;
public ViewHolder(View itemView) {
super(itemView);
cvCard=(CardView)itemView.findViewById(R.id.cv_card);
personName=(TextView)itemView.findViewById(R.id.txt_person_name);
personAge=(TextView)itemView.findViewById(R.id.txt_person_age);
alarmImageButton=(ImageButton)itemView.findViewById(R.id.alarm_image_button);
}
如果有人可以帮助我发现此错误,这将是一个很大的帮助...
It will be a great help if someone can help me to find this error...
推荐答案
我只是无法正确解决这个问题.第一张卡如何第七张卡 视图是相互联系的?
I just couldn't figure this out properly. How the 1st card 7th card views are connected with each other?
使用RecyclerView
的要点是View
正在回收.
您设置了ImageButton
的colorFilter,但是在回收的View
中也可以使用相同的按钮.
The point of using a RecyclerView
is that the View
s are getting recycled.
You set the colorFilter of the ImageButton
, but that same button may be used in a recycled View
also.
您应该存储RecyclerView
行的单击状态,并相应地在onBindViewHolder()
中的每个ImageButton
上设置colorFilter.
You should store the clicked state of your RecyclerView
rows and set the colorFilter accordingly on each ImageButton
in onBindViewHolder()
.
例如:
// a HashSet to store the clicked positions
private HashSet<Integer> clickedPositions = new HashSet<>();
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ViewHolder mHolder = holder;
mHolder.personName.setText(persons.get(position).name);
mHolder.personAge.setText((persons.get(position).age));
mHolder.cvCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "card number:" + String.valueOf(position),
Toast.LENGTH_SHORT).show();
}
});
if (clickedPositions.contains(mHolder.getAdapterPosition())) {
// the current item is clicked, change its color
mHolder.alarmImageButton.setColorFilter(Color.argb(255, 255, 255, 255));
} else {
// else change it to another color
mHolder.alarmImageButton.setColorFilter(Color.argb(0, 0, 0, 0));
}
mHolder.alarmImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "button:" + String.valueOf(position),
Toast.LENGTH_SHORT).show();
// if the item is already clicked, remove it from the clicked items
if (!clickedPositions.remove(mHolder.getAdapterPosition())) {
// if not clicked, add it to the clicked items
clickedPositions.add(mHolder.getAdapterPosition());
}
// and notify the Adapter that the item is changed
notifyItemChanged(mHolder.getAdapterPosition());
}
});
}
这篇关于如何在RecyclerView中更改分配给ImageButton的矢量的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!