AlexZwh
AlexZwh

Reputation: 53

Child RecyclerView item intercepts click event of parent RecyclerView item

Parent item click

Child item click

I have a parent RecyclerView with nested child RecyclerView items inside. The problem is that I only click on the non-child RecyclerView part of the parent item to trigger the click event of the parent RecyclerView. Clicking on the child RecyclerView item will intercept the click event. I would like to know how to cancel the click interception of the child RecyclerView item and pass the click on it. Go to the parent RecyclerView item so that I can display the wave effect and trigger the unique click event regardless of the area in the parent RecyclerView item.

Here is my adapter code:

parentAdapter.setOnItemClickListener { _, _, position ->
    val bundle = Bundle()
    bundle.putSerializable("record", recordAdapter.data[position])
    ActivityUtils.startActivity(bundle, RecordDetailActivity::class.java)
}

parent adapter

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)
    }
}

child adapter

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>) {
        ...
    }
}

Upvotes: 5

Views: 1040

Answers (1)

Eugene Troyanskii
Eugene Troyanskii

Reputation: 892

You can add some view (let's call it @+id/item_click_area) at the bottom of your root layout to item_record. Or play with the elevation param. The main goal is to set item_click_area upper than child ReciclerView. And set onClick on item_click_area View.

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

Upvotes: 0

Related Questions