gpt4 book ai didi

android - 如何更改分配给 RecyclerView 中的 ImageButton 的矢量颜色?

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

我有一个 recyclerview,它显示了几 (8) 个卡片 View 。在每个卡片 View 中,有几个元素,如文本、图像按钮等。在 Adapter 类中(RvAdapter extends RecyclerView.Adapter)我有如下代码。我想要做的是使用同一按钮的 OnClickListener 更改分配给图像按钮的 Vector (.xml) 的颜色。

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 个卡片 View 。单击第一个 cardview 的图像按钮的图像按钮时,它会将颜色更改为白色。但错误是它也改变了第 7 张卡片的图像按钮颜色。当点击第二张卡片 View 的图像按钮时,它也会更改第 8 张卡片的图像按钮颜色。但是, toast 工作正常。它表明点击事件已经正确识别了每个cardview和imagebutton的位置。

我只是无法正确解决这个问题。第 1 张卡片第 7 张卡片 View 如何相互关联?

这是我的 ViewHolder 类

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);
}

如果有人能帮我找到这个错误,那将是一个很大的帮助......

最佳答案

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 中使用。

您应该存储 RecyclerView 行的点击状态,并在 onBindViewHolder() 中相应地为每个 ImageButton 设置 colorFilter。

例如:

// 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());
}
});
}

关于android - 如何更改分配给 RecyclerView 中的 ImageButton 的矢量颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858440/

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