使用回收器视图在Android Studio中使用回收按钮状态
我使用回收器视图中的无线电按钮有问题,收音机按钮可以正常工作,但是当我回到上一个片段时,然后返回包含回收库的片段,在最后一个项目上检查了无线电按钮状态。我点击了,我想重置单选按钮的状态。这是示例的屏幕截图。
是我第一次将recyclerview的片段导航到一个片段:预期
这 是当我单击其中一种选择时,回到上一个片段,然后返回到包括回收库的片段:当前状态
这是我的片段,适配器和视图持有人
片段中的代码:
private fun initializeRecyclerView(){
val transactionTypeLayoutManager = GridLayoutManager(context, 2)
val list = TransactionType.values().toList()
val transactionTypeAdapter = ManualMixTransactionTypeAdapter(list)
binding?.recyclerViewTransactionType?.layoutManager = transactionTypeLayoutManager
binding?.recyclerViewTransactionType?.adapter = transactionTypeAdapter
transactionTypeAdapter.onTransactionTypeClicked = {
viewModel.setTransactionType(it.name)
Toast.makeText(context, "${it.name}", Toast.LENGTH_SHORT).show()
}
}
适配器:
var onTransactionTypeClicked: ((TransactionType) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionTypeViewHolder {
val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
val binding: ViewholderTransactionTypeBinding = ViewholderTransactionTypeBinding.inflate(layoutInflater, parent, false)
return TransactionTypeViewHolder(binding)
}
override fun onBindViewHolder(holder: TransactionTypeViewHolder, position: Int) {
holder.binding.rbTransactionType.setText(transactionType[position].key)
holder.render(
data = transactionType[position],
dataIndex = position,
adapterListener = {
val oldTransactionType = transactionType.find { transactionType -> transactionType.isItemSelected }
oldTransactionType?.apply { isItemSelected = false }
val oldPosition = transactionType.indexOf(oldTransactionType)
notifyItemChanged(oldPosition)
},
updateListener = { newPosition -> notifyItemChanged(newPosition)},
clickListener = onTransactionTypeClicked
)
}
视图持有人:
fun render(
data: TransactionType,
dataIndex: Int,
adapterListener: (() -> Unit),
updateListener: ((Int) -> Unit),
clickListener: ((TransactionType) -> Unit)?
) {
val onClickListener: View.OnClickListener = View.OnClickListener {
adapterListener.invoke()
data.isItemSelected = true
updateListener.invoke(dataIndex)
clickListener?.invoke(data)
}
binding.vhTransactionType.setOnClickListener(onClickListener)
binding.rbTransactionType.setOnClickListener(onClickListener)
binding.rbTransactionType.isChecked = data.isItemSelected
}
任何解决方案都很棒,非常感谢!
I have a problem using radio button inside a recycler view, the radio button works fine, but when I go back to the previous fragment, and go back to the fragment where the recyclerview is included, the radio button state is checked on the last item that I clicked, I want to reset the state of the radio button. Here's a screenshot of the example.
Here is where I navigate into a fragment that include the recyclerView for the first time : Expected
And here is when I clicked one of the choices, go back to the previous fragment, and go back to the fragment where the recyclerView is included : Current State
Here is my code in fragment, adapter, and viewholder
Fragment :
private fun initializeRecyclerView(){
val transactionTypeLayoutManager = GridLayoutManager(context, 2)
val list = TransactionType.values().toList()
val transactionTypeAdapter = ManualMixTransactionTypeAdapter(list)
binding?.recyclerViewTransactionType?.layoutManager = transactionTypeLayoutManager
binding?.recyclerViewTransactionType?.adapter = transactionTypeAdapter
transactionTypeAdapter.onTransactionTypeClicked = {
viewModel.setTransactionType(it.name)
Toast.makeText(context, "${it.name}", Toast.LENGTH_SHORT).show()
}
}
Adapter :
var onTransactionTypeClicked: ((TransactionType) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionTypeViewHolder {
val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
val binding: ViewholderTransactionTypeBinding = ViewholderTransactionTypeBinding.inflate(layoutInflater, parent, false)
return TransactionTypeViewHolder(binding)
}
override fun onBindViewHolder(holder: TransactionTypeViewHolder, position: Int) {
holder.binding.rbTransactionType.setText(transactionType[position].key)
holder.render(
data = transactionType[position],
dataIndex = position,
adapterListener = {
val oldTransactionType = transactionType.find { transactionType -> transactionType.isItemSelected }
oldTransactionType?.apply { isItemSelected = false }
val oldPosition = transactionType.indexOf(oldTransactionType)
notifyItemChanged(oldPosition)
},
updateListener = { newPosition -> notifyItemChanged(newPosition)},
clickListener = onTransactionTypeClicked
)
}
Viewholder :
fun render(
data: TransactionType,
dataIndex: Int,
adapterListener: (() -> Unit),
updateListener: ((Int) -> Unit),
clickListener: ((TransactionType) -> Unit)?
) {
val onClickListener: View.OnClickListener = View.OnClickListener {
adapterListener.invoke()
data.isItemSelected = true
updateListener.invoke(dataIndex)
clickListener?.invoke(data)
}
binding.vhTransactionType.setOnClickListener(onClickListener)
binding.rbTransactionType.setOnClickListener(onClickListener)
binding.rbTransactionType.isChecked = data.isItemSelected
}
Any solution would be great, thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在设置正确的值之前,您需要在单选按钮上清除选择。
我将以Java示例来解释这一概念。
这样,
radiogroup
清除了其选择,准备接收一个输入,该输入将触发正确的radiobutton
。但是,您需要意识到,这可能会将您的
On CheckedChanged
听众称为听众,并可能带来意外的结果。但是,解决了由此产生的问题。您可以在Kotlin做到这一点
You need to clear the selection on the radio button before setting its correct value.
I will use a java example to explain the concept.
This way, the
RadioGroup
clears its selection, ready to receive an input that will trigger the correctRadioButton
as checked.But you need to be aware that this might call your
onCheckedChanged
listeners and could bring unexpected outcomes. But there is a work around for the resulting problem.You can do this in kotlin