简体   繁体   English

在 Android Studio 中使用 Recycler View 重置单选按钮状态

[英]Reset Radio Button State using Recycler View in Android Studio

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.我在回收站视图中使用单选按钮时遇到问题,单选按钮工作正常,但是当我返回上一个片段并返回包含 recyclerview 的片段时,在最后一项上检查单选按钮状态我点击了,我想重置单选按钮的状态。 Here's a screenshot of the example.这是示例的屏幕截图。

Here is where I navigate into a fragment that include the recyclerView for the first time : Expected这是我第一次导航到包含 recyclerView 的片段的地方:预期

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这是当我单击其中一个选项时,返回上一个片段,然后返回包含 recyclerView 的片段:当前状态

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!任何解决方案都会很棒,非常感谢!

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.我将使用一个 java 示例来解释这个概念。

holder.radioGroup.clearCheck()

This way, the RadioGroup clears its selection, ready to receive an input that will trigger the correct RadioButton as checked.这样, RadioGroup清除其选择,准备接收将触发正确RadioButton的输入。

But you need to be aware that this might call your onCheckedChanged listeners and could bring unexpected outcomes.但是您需要注意,这可能会调用您的onCheckedChanged侦听器,并可能带来意想不到的结果。 But there is a work around for the resulting problem.但是对于由此产生的问题有一个解决方法。

// Nullify listener
holder.radioGroup.setOnCheckedChangeListener(null);

// Clear selection
holder.radioGroup.clearCheck();

// Reset listener after selection has been cleared
holder.radioGroup.setOnCheckedChangeListener(checkedChangeListener);

You can do this in kotlin您可以在 kotlin 中执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM