android-fragments 使用Android Studio中的回收器视图重置单选按钮状态

9gm1akwq  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(165)

我在使用recycler视图中的单选按钮时遇到了问题,单选按钮工作正常,但是当我返回到上一个片段,返回到包含recyclerview的片段时,单选按钮的状态在我单击的最后一个项目上被选中,我想重置单选按钮的状态。下面是这个示例的屏幕截图。
下面是我第一次导航到包含recyclerView的片段的位置:Expected
这里是当我单击其中一个选项时,返回到上一个片段,并返回到包含recyclerView的片段:Current State
下面是我的代码片段、适配器和视图持有者
片段:

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
}

任何解决方案都很好,非常感谢!

clj7thdc

clj7thdc1#

您需要先清除单选按钮上的选择,然后再设置其正确值。
我将用一个java例子来解释这个概念。

holder.radioGroup.clearCheck()

这样,RadioGroup清除其选择,准备好接收将触发正确的RadioButton的输入,如所检查的。
但是您需要注意,这可能会调用您的onCheckedChanged侦听器,并可能带来意想不到的结果。

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

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

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

你可以在Kotlin做这个

相关问题