我有50个recyclerview项目,当我点击项目时,现在是绿色的 它变为红色,当我再次点击然后它变回绿色下面是代码如何实现它
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
((ItemViewHolder)holder).Title.setText(position+1+"");
((ItemViewHolder)holder).imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status==0){
status=1;
((ItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_social_person);//red icon
}else {
status=0;
((ItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_social_person_green);//green icon
}
}
});
} else {
((progressHolder) holder).progressBar.setIndeterminate(true);
}
}
现在的问题是当我点击第一个项目时它会变成红色图标但是当我点击第二个时它不会变成红色图标当我再次点击它再次变为红色图标如何解决这个
答案 0 :(得分:1)
public SparseBooleanArray array = new SparseBooleanArray();
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ItemViewHolder) {
((ItemViewHolder)holder).Title.setText(position+1+"");
((ItemViewHolder)holder).imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean status = array.get(position);
if (!status){
array.put(position,!status);
((ItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_social_person);//red icon
}else {
array.put(position,!status);
((ItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_social_person_green);//green icon
}
}
});
} else {
((progressHolder) holder).progressBar.setIndeterminate(true);
}
}