android - 如何在整个recyclerview行上单击以选中复选框

标签 android kotlin android-recyclerview android-checkbox

现在我注册每个复选框,每次单击y时都按如下所示

 inner class OrdersInnerViewHolder(itemView: View): BaseViewHolder<Cart>(itemView){
        override fun bind(item: Cart, position: Int) {

            itemView.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                item.isChecked = isChecked
                if(isChecked){
                 map.put(position,true)
                }else{
                    map.remove(position,true)
                }
            }
        }
    }

我计划单击整个行以选中或取消选中我的项目,现在我知道如何执行对整个行的单击,但是不知道如何设置选中的更改后的侦听器
 inner class OrdersInnerViewHolder(itemView: View): BaseViewHolder<Cart>(itemView){
            override fun bind(item: Cart, position: Int) {

            itemView.setOnClickListener { 
                //How to perform checkbox check and uncheck clicking on the entire row instead of the checkbox ?
            }
                itemView.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    item.isChecked = isChecked
                    if(isChecked){
                     map.put(position,true)
                    }else{
                        map.remove(position,true)
                    }
                }
            }
        }

由于该复选框具有选中的侦听器,因此单击整个 View 时无法与之交互

我打算让我的用户单击该复选框,然后单击整行以选择或取消选择此复选框

XML格式
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:weightSum="3"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:text="Test " />

    <TextView
        android:id="@+id/item_qty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Qty: 2" />

    <TextView
        android:id="@+id/item_price"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        tools:text="$100.00"/>

</LinearLayout>

有任何想法吗?

最佳答案

将OnClickListener添加到可切换CheckBox的父 View 。仍将调用OnCheckedChangeListener来响应父 View 的单击侦听器更改选中状态:

itemView.setOnClickListener {
    itemView.checkBox.isChecked = !itemView.checkBox.isChecked
}

为了使父 View 看起来和行为正确,可以将这些 View 添加到XML的元素中:
android:background="?android:attr/selectableItemBackground"
android:clipToPadding="false"
android:focusable="true"

IIRC将OnCheckedChangeListener添加到CheckBox会自动使其可单击。您可能会考虑在设置侦听器之后立即设置itemView.checkBox.isClickable = false,这样,即使您点击CheckBox,整个列表项也会显示涟漪效应。

注意事项:不要忘记在onBindView中设置CheckBox的初始状态:
itemView.checkBox.isChecked = item.isChecked

当它滚动到屏幕上时,它将具有不可预测的状态,否则!

关于android - 如何在整个recyclerview行上单击以选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61621599/

相关文章:

java - RecyclerView 并不总是显示结果

android - 映射鼠标右键以在 Jelly Bean 中向后遍历

android - 如何在多个模块中使用 Koin?

android - 如何将图像放入 AlertDialog?安卓

abstract-syntax-tree - 如何获得 Kotlin AST?

android - Unresolved reference kotlinx

android - 一个布局中的两个 RecyclerView 在彼此下方

java - 在 BottomNavigationView 项目触摸时,Fragment 中的 RecyclerView 有时为空

Android 1.6 setZOrderOnTop替代方案

java - Android项目将部分Java代码改成C++