Android Adapter单选UI问题:切换选中项时如何自动取消前项选中状态
解决方案:实现单选分支UI并自动取消上一项选中状态
针对你的需求,这里有几种不同场景下的Kotlin实现方案,你可以根据自己的UI结构选择最合适的:
方案1:自定义View(如TextView)手动管理选中状态
如果你的分支项是自定义的TextView或其他View,核心思路是记录上一个选中的View实例,点击新项时先清除上一项的选中标记,再设置当前项的状态:
// 在Activity/Fragment中定义变量保存当前选中项 private var lastSelectedItem: TextView? = null // 给每个分支项设置点击事件(以4个TextView为例) listOf(tvBranch1, tvBranch2, tvBranch3, tvBranch4).forEach { item -> item.setOnClickListener { // 1. 取消上一项的选中状态 lastSelectedItem?.apply { setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0) // 清除选中图标 // 可选:恢复默认文本样式(比如颜色、字体) setTextColor(ContextCompat.getColor(context, R.color.text_normal)) } // 2. 设置当前项的选中状态 it.apply { setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_selected_check, 0) // 添加选中图标 setTextColor(ContextCompat.getColor(context, R.color.text_selected)) } // 3. 更新记录的选中项 lastSelectedItem = it as TextView } }
方案2:RecyclerView列表场景下的高效实现
如果你的分支项是RecyclerView的列表项,推荐通过维护选中位置来管理状态,避免全量刷新:
class BranchAdapter(private val branchList: List<String>) : RecyclerView.Adapter<BranchAdapter.ViewHolder>() { // 记录当前选中的位置,默认-1表示未选中 private var selectedPosition = -1 inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val branchText: TextView = itemView.findViewById(R.id.tv_branch) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_branch, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.branchText.text = branchList[position] // 根据选中状态设置UI if (position == selectedPosition) { holder.branchText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_selected_check, 0) holder.branchText.typeface = Typeface.DEFAULT_BOLD } else { holder.branchText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0) holder.branchText.typeface = Typeface.DEFAULT } // 列表项点击事件 holder.itemView.setOnClickListener { val previousPosition = selectedPosition selectedPosition = holder.adapterPosition // 仅刷新之前选中项和当前项,提升性能 notifyItemChanged(previousPosition) notifyItemChanged(selectedPosition) } } override fun getItemCount() = branchList.size }
方案3:利用系统RadioGroup实现(最省心)
如果你的UI允许使用系统控件,直接用RadioGroup + RadioButton是最省事的——系统已经内置了单选逻辑,你只需要自定义RadioButton的样式即可:
1. 创建选中状态的Drawable选择器(drawable/radio_selector.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_selected_check" android:state_checked="true"/> <item android:drawable="@drawable/ic_unselected_check" android:state_checked="false"/> </selector>
2. 布局中使用RadioGroup包裹RadioButton
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="分支1" android:button="@drawable/radio_selector" android:padding="16dp"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="分支2" android:button="@drawable/radio_selector" android:padding="16dp"/> <!-- 其他分支项 --> </RadioGroup>
这种方式不需要自己写任何单选逻辑,系统会自动处理选中状态的切换,非常适合快速实现需求。
内容的提问来源于stack exchange,提问作者Abdulrahman
相关产品推荐
相关产品推荐

