使用回收器视图在Android Studio中使用回收按钮状态

发布于 2025-02-07 18:44:41 字数 2877 浏览 1 评论 0原文

我使用回收器视图中的无线电按钮有问题,收音机按钮可以正常工作,但是当我回到上一个片段时,然后返回包含回收库的片段,在最后一个项目上检查了无线电按钮状态。我点击了,我想重置单选按钮的状态。这是示例的屏幕截图。

是我第一次将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2025-02-14 18:44:41

在设置正确的值之前,您需要在单选按钮上清除选择。

我将以Java示例来解释这一概念。

holder.radioGroup.clearCheck()

这样,radiogroup清除了其选择,准备接收一个输入,该输入将触发正确的radiobutton

但是,您需要意识到,这可能会将您的On CheckedChanged听众称为听众,并可能带来意外的结果。但是,解决了由此产生的问题。

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

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

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

您可以在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.

holder.radioGroup.clearCheck()

This way, the RadioGroup clears its selection, ready to receive an input that will trigger the correct RadioButton 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.

// 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文