The following code mainly solves two problems:
Solution to ViewPager Nested Vertical Recycler View Nested Horizontal Recycler View Sliding Horizontally to the End without ViewPager Sliding Horizontally
Solution: Inherit Recycler View, rewrite dispatch TouchEvent, and determine whether to call getParent (). Request Disallow Intercept TouchEvent to prevent parent view from intercepting click events according to the direction of ACTION_MOVE
@Override public boolean dispatchTouchEvent(MotionEvent ev) { /*---Solve the problem of vertical view Pager nested straight Recycler View nested horizontal Recycler View sliding horizontally to the end without sliding ViewPager start - -*/ ViewParent parent =this; while(!((parent = parent.getParent()) instanceof ViewPager));// Loop lookup viewPager parent.requestDisallowInterceptTouchEvent(true); /*---Solve the problem of vertical view Pager nested straight Recycler View nested horizontal Recycler View sliding horizontally to the end without sliding ViewPager start - -*/ return super.dispatchTouchEvent(ev); }
Solving the problem of horizontal Recycler View's lateral sliding smoothly nested in vertical Recycler View
Solution: Loop to see if Father View is ViewPager, and if so, prevent Father View from intercepting events
@Override public boolean dispatchTouchEvent(MotionEvent ev) { /*--------Solve the problem of vertical Recycler View nested horizontal Recycler View sliding smoothly in horizontal direction. start - --------------------------------------------------------------------------------------*/ int x = (int) ev.getRawX(); int y = (int) ev.getRawY(); int dealtX = 0; int dealtY = 0; switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: dealtX = 0; dealtY = 0; // Guarantee that Sub View receives Action_move events getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: dealtX += Math.abs(x - lastX); dealtY += Math.abs(y - lastY); // Log.i("dispatchTouchEvent", "dealtX:=" + dealtX); // Log.i("dispatchTouchEvent", "dealtY:=" + dealtY); // Here is the judgment basis of sufficient interception, which is left-right sliding. Readers can intercept or not according to their own logic. if (dealtX >= dealtY) { getParent().requestDisallowInterceptTouchEvent(true); } else { getParent().requestDisallowInterceptTouchEvent(false); } lastX = x; lastY = y; break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_UP: break; } /*--------Solve the problem of vertical Recycler View nested horizontal Recycler View horizontal sliding not smooth end - --------------------------------------------------------------------------------------------------*/ return super.dispatchTouchEvent(ev); }
Complete code
package com.leo.demo.view; import android.content.Context; import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewParent; /** * Created by kangyi on 2018/6/15. */ public class HorizontalRecyclerView extends RecyclerView { int lastX = 0; int lastY = 0; public HorizontalRecyclerView(Context context) { super(context); } public HorizontalRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public HorizontalRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { /*--------Solve the problem of vertical Recycler View nested horizontal Recycler View sliding smoothly in horizontal direction. start - --------------------------------------------------------------------------------------*/ int x = (int) ev.getRawX(); int y = (int) ev.getRawY(); int dealtX = 0; int dealtY = 0; switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: dealtX = 0; dealtY = 0; // Guarantee that Sub View receives Action_move events getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: dealtX += Math.abs(x - lastX); dealtY += Math.abs(y - lastY); // Log.i("dispatchTouchEvent", "dealtX:=" + dealtX); // Log.i("dispatchTouchEvent", "dealtY:=" + dealtY); // Here is the judgment basis of sufficient interception, which is left-right sliding. Readers can intercept or not according to their own logic. if (dealtX >= dealtY) { getParent().requestDisallowInterceptTouchEvent(true); } else { getParent().requestDisallowInterceptTouchEvent(false); } lastX = x; lastY = y; break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_UP: break; } /*--------Solve the problem of vertical Recycler View nested horizontal Recycler View horizontal sliding not smooth end - --------------------------------------------------------------------------------------------------*/ /*---Solve the problem of ViewPager nested vertical Recycler View nested horizontal Recycler View sliding horizontally to the end without sliding ViewPager start - -*/ ViewParent parent =this; while(!((parent = parent.getParent()) instanceof ViewPager));// Loop lookup viewPager parent.requestDisallowInterceptTouchEvent(true); /*---Solve the problem of ViewPager nested vertical Recycler View nested horizontal Recycler View sliding horizontally to the end without sliding ViewPager start - -*/ return super.dispatchTouchEvent(ev); } }
Take a walk with a little gift, come to a brief book and pay attention to me
Author: A Private Visit to Kangxi Weibo
Link: https://www.jianshu.com/p/4c87e0b6d16c
Source: Brief Book
The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.