Search code examples
androidandroid-recyclerviewcarouselhorizontal-scrolling

Parent RecyclerView picks up gestures that were intended to be for the nested RecyclerView


I have a vertical RecyclerView and a nested horizontal RecyclerView as one of its children. The inner view is used as an image carousel with a PagerSnapHelper. I'm getting a less than ideal behavior when I try to scroll the inner view horizontally. Sometimes, the parent view picks up the gesture and scrolls vertically even though my intention was to scroll the inner view horizontally. Is there a way to configure the default scroll detector such that horizontal scrolling is smoother?


Solution

  • Can you try using this RecyclerView with overriden method as a parent RecyclerView:

    public class CustomRecyclerView extends RecyclerView {
    
        private int mTouchSlop;
        private float mPrevX;
    
        public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent e) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPrevX = MotionEvent.obtain(e).getX();
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    final float eventX = e.getX();
                    float xDiff = Math.abs(eventX - mPrevX);
    
                    if (xDiff > mTouchSlop) {
                        return false;
                    }
            }
    
            return super.onInterceptTouchEvent(e);
        }
    }
    

    example in xml

    <com.example.stackoverflow.CustomRecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />