我有一个回收视图,显示了几个(8)卡片视图。在每个cardview中,有几个元素,如文本,图像按钮等。在Adapter类中(RvAdapter扩展了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张卡片视图。单击第一个cardview的图像按钮的图像按钮时,它会将颜色更改为白色。但错误是它也改变了第7张卡片的图像按钮颜色。单击第二张cardview的图像按钮时,它也会更改第8张卡片的图像按钮颜色。然而,Toast工作得很好。它显示click事件已正确识别每个cardview和imagebutton的位置。
我无法正确理解这一点。第一张卡的第7张卡片视图是如何相互连接的?
这是我的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);
}
如果有人可以帮我找到这个错误,那将是一个很好的帮助......
答案 0 :(得分:2)
我无法正确理解这一点。如何第1张牌第7张牌 观点相互联系?
使用RecyclerView
的重点是View
正在被回收。
您设置ImageButton
的colorFilter,但同样的按钮也可以在回收的View
中使用。
您应该存储RecyclerView
行的点击状态,并相应地为ImageButton
中的每个onBindViewHolder()
设置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());
}
});
}
答案 1 :(得分:0)
将ids存储在sparsebooleanarray中。如果颜色改变则存储为真,否则为假。
将这些方法添加到适配器。
private SparseBooleanArray mSelectedItemsIds;
//Methods to do Selection
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
//Remove selected selections
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
//Put or delete selected position into SparseBooleanArray
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
// notifyItemChangedAtPosition(position);
notifyDataSetChanged();
}
&#13;
然后在需要时调用它。