在Android中选择取消选择项的CarView Group

编程入门 行业动态 更新时间:2024-10-28 14:37:18
本文介绍了在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

本文发布于:2023-07-19 07:21:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   CarView   Group

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!