我想实现这个 UI,让用户一次选择一个分支。 我现在正在做的是在用户选择每个项目时为每个项目中的文本设置 setCompoundDrawablesWithIntrinsicBounds。
问题是“如果我选择项目编号 1,然后选择项目编号 4,如何从项目编号 1 中删除选择?我不想再次单击项目编号 1 以删除选择”
请注意,此屏幕使用户只能选择一项。
这是我在 Kotlin 中的代码,但如果您提出 JAVA 中的解决方案也没关系:
var selectedBranch = false
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.packageNumber.text = branches?.get(position)?.nameEn ?: ""
check = ContextCompat.getDrawable(context, R.drawable.ic_select_branch)
holder.itemView.setOnClickListener {
if(!selectedBranch) {
holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
selectedBranch = true
}
else {
holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
selectedBranch = false
}
}
}
最佳答案
1.创建一个包含
的模型
类
String text;
boolean isTextSelected;
2.单击适配器中的项目时,更新
您的模型类“isTextSelected”字段。
3.通知
您的适配器。
关于java - 从适配器中选择 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019636/