Timmy Simons
Timmy Simons

Reputation: 599

Vertical RecyclerView reacts to scroll on nested Horizontal RecyclerView

enter image description here

In my activity, I have ViewPager consisting of fragments. I have disabled swipe motion on ViewPager using CustomViewPager.

My fragment layout has Vertical RecyclerView and inside this RecyclerView there are multiple Horizontal RecyclerViews.

To ensure that my AppBarLayout responds to scroll behaviour properly, I have programmatically set nestedScrollingEnabled to false on each Horizontal RecyclerView.

P.S. I'm using recylerview version 25.4.0

The problem is as you can see in gif attached above, to scroll horizontal recyclerview the touch should be perfectly sideways, even for a slightly slant motion on horizontal recyclerview, the vertical recyclerview picks up that event and scrolls the page vertically UP & DOWN. How to resolve this?

CustomViewPager.java

public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

Fragment Layout

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_productoverview"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/windowBackground"/>

Layout of items inside recyclerview_productoverview(Vertical RecyclerView)

<android.support.constraint.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.support.constraint.ConstraintLayout
        android:id="@+id/layout_section_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#ebedff"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <experttag.nurserylive.util.ui.widget.WhitneySemiBoldTextView
            android:id="@+id/textview_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="TOP FEATURED"
            android:textColor="#3f4266"
            android:textSize="16sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp"
            app:layout_constraintVertical_bias="0.19"
            android:layout_marginLeft="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginRight="16dp"
            app:layout_constraintRight_toRightOf="parent"/>

        <experttag.nurserylive.util.ui.widget.WhitneySemiBoldTextView
            android:id="@+id/textview_item_viewall"
            style="@style/AppTheme.TextLink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#3f4266"
            android:text="@string/btn_view_all_caps"
            android:layout_marginRight="16dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBaseline_toBaselineOf="@+id/textview_item_name"/>
    </android.support.constraint.ConstraintLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_item_section"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout_section_header"/>

</android.support.constraint.ConstraintLayout>

Horizontal RecyclerView: @id/recyclerview_item_section

Upvotes: 0

Views: 856

Answers (1)

Timmy Simons
Timmy Simons

Reputation: 599

Solved this using BetterRecyclerView

see demo

read explanation

BetterRecyclerView.java

Upvotes: 1

Related Questions