在Android中选择取消选择项的CarView Group [英] CarView Group with select an deselecting items in android
本文介绍了在Android中选择取消选择项的CarView Group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此处,将分支项目作为按钮在recycleview内的cardview上对齐.我需要在每个cardview上单击一次,并且颜色应该更改,但要点是,每次我单击另一张cardview之前,都应取消选择所选择的cardView.我什至没有实现这一点的逻辑.请提供一种简便的方法
Here the Branch items are aligned on a cardview within the recycleview as a button. I need to implement a click on each cardview and the color should change, but the point is that each time i click another cardView the selected cardView before should deselected. I don't even have a logic to implement this. Please help with an easy method
推荐答案
您可以尝试一下,
public class yourRecyclerViewAdapter extends RecyclerView.Adapter<yourRecyclerViewAdapter.yourViewHolder> {
private static int lastCheckedPos = 0;
... ...
public void onBindViewHolder(ViewHolder holder, final int position) {
if(position == lastCheckedPos) {
holder.cardView..setCardBackgroundColor(Color.RED); //Define the re
} else {
holder.cardView..setCardBackgroundColor(Color.WHITE);
}
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int prevPos = lastCheckedPos;
lastCheckedPos = position;
notifyItemChanged(prevPos);
notifyItemChanged(lastCheckedPos);
}
});
}
... ...
}
它可能会为实现提供基本逻辑
It may give a base logic on the implementation
这篇关于在Android中选择取消选择项的CarView Group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文