当我单击项目时,我现在有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);
}
}
现在的问题是,当我单击第一项时它变成红色图标,但是当我单击第二项时它没有变成红色图标,当我第二次再次单击时它变成红色图标如何解决这个问题
最佳答案
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);
}
}
关于android - 将交替的onClickListener设置为Recyclerview项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34471827/