我有一个父 RecyclerView,里面有嵌套的子 RecyclerView 项目。问题是我只点击父项的非子RecyclerView部分才能触发父RecyclerView的点击事件。单击子 RecyclerView 项将拦截单击事件。我想知道如何取消子RecyclerView项的点击拦截并传递对其的点击。转到父RecyclerView项,这样无论父RecyclerView项中的区域如何,我都可以显示波浪效果并触发唯一的单击事件。
这是我的适配器代码:
parentAdapter.setOnItemClickListener { _, _, position ->
val bundle = Bundle()
bundle.putSerializable("record", recordAdapter.data[position])
ActivityUtils.startActivity(bundle, RecordDetailActivity::class.java)
}
父级适配器
class RecordAdapter : BaseQuickAdapter<Record, BaseViewHolder>(R.layout.item_record) {
override fun convert(helper: BaseViewHolder, item: Record) {
...
val adapter = ChildAdapter()
val recyclerView = helper.getView<RecyclerView>(R.id.item_record_product_rv)
recyclerView.layoutManager = LinearLayoutManager(mContext)
recyclerView.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL))
adapter.bindToRecyclerView(recyclerView)
adapter.setNewData(item.productInfo)
}
}
儿童适配器
class RecordProductAdapter : BaseQuickAdapter<RecordProduct<String, String, Double, Int>, BaseViewHolder>(R.layout.item_record_product) {
override fun convert(helper: BaseViewHolder, item: RecordProduct<String, String, Double, Int>) {
...
}
}
您可以在根布局底部向 item_record 添加一些视图(我们称之为 @+id/item_click_area)。或者使用海拔参数。主要目标是将 item_click_area 设置为高于子 ReciclerView。并在item_click_area视图上设置onClick。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@drawable/bg_round_12_stroke_grey"
android:paddingBottom="24dp">
<TextView
android:id="@+id/item_pending_order_status"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:background="@drawable/bg_round_8_light_grey"
android:gravity="center"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/orders_status_past"
android:textColor="@color/textColorLight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_pending_order_ttn_ic" />
<TextView
android:id="@+id/item_pending_order_price"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:gravity="center"
android:paddingStart="16dp"
android:paddingEnd="10dp"
android:textColor="@color/textColorLight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_pending_order_id"
tools:text="$ 24.99" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/item_order_products_rv"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginTop="24dp"
android:clipToPadding="false"
android:orientation="horizontal"
android:paddingStart="21dp"
android:paddingEnd="21dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_pending_order_status" />
<View
android:id="@+id/item_click_area"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>