Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

android - Nested RecyclerView not scrolling

I'm having a problem adding a recycler view inside another recycler view. The child recycler is inside a CardView and the CardView is inside the parent recycler view. I tried all solutions on the internet and no use. I want the child recycler view to scroll vertically while the parent recycler view also scroll vertically.

Parent Recycler view:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundPrimary"
    tools:context=".BidsCenter">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/bidCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        />
</FrameLayout>

Child recycler view:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:animateLayoutChanges="true"
    android:clickable="true"
    android:elevation="3dp"
    android:focusable="true"
    android:orientation="vertical"
    app:cardCornerRadius="10dp">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <RelativeLayout
            android:id="@+id/r0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/itemPreviewInCenter"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_margin="3dp"
                android:scaleType="fitCenter"
                android:src="@android:drawable/ic_menu_report_image" />

            <TextView
                android:id="@+id/itemTitleInCenter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_margin="3dp"
                android:layout_toStartOf="@+id/itemPreviewInCenter" />

            <TextView
                android:id="@+id/totalBidPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@+id/numberOfBids"
                android:background="@drawable/bid_total"
                android:clickable="false" />

            <TextView
                android:id="@+id/latestBidDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_margin="3dp"
                android:layout_toEndOf="@id/bidMenu" />

            <TextView
                android:id="@+id/numberOfBids"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerVertical="true"
                android:layout_margin="3dp"
                android:layout_toEndOf="@+id/latestBidDate"
                android:background="@drawable/new_message_count"
                android:gravity="center"
                android:textColor="@color/text"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/bidMenu"
                style="?android:attr/actionOverflowButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true" />
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/showBidders"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/r0"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_margin="3dp" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/latestBidsInCenter"
                android:layout_width="match_parent"
                android:layout_height="120dp" />
        </LinearLayout>
    </RelativeLayout>
</androidx.cardview.widget.CardView>

here are some screenshots

parent: enter image description here

child: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Editted

you have to disable parent recyclerView touch event while scrolling child Recyclerview

latestBidsInCenter.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == child.getId()) {
            if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP) {
                bidCenter.requestDisallowInterceptTouchEvent(false);
            } else {
                bidCenter.requestDisallowInterceptTouchEvent(true);
            }
        }

        return super.onTouchEvent(event);
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...