如下代碼主要解決兩個問題:
解決ViewPager嵌套垂直RecyclerView嵌套水平RecyclerView橫向滑動到底后不滑動ViewPager
解決思路: 繼承RecyclerView,重寫dispatchTouchEvent蚂蕴,根據(jù)ACTION_MOVE的方向判斷是否調(diào)用getParent().requestDisallowInterceptTouchEvent去阻止父view攔截點擊事件
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/*---解決垂ViewPager嵌套直RecyclerView嵌套水平RecyclerView橫向滑動到底后不滑動ViewPager start ---*/
ViewParent parent =this;
while(!((parent = parent.getParent()) instanceof ViewPager));// 循環(huán)查找viewPager
parent.requestDisallowInterceptTouchEvent(true);
/*---解決垂ViewPager嵌套直RecyclerView嵌套水平RecyclerView橫向滑動到底后不滑動ViewPager start ---*/
return super.dispatchTouchEvent(ev);
}
解決垂直RecyclerView嵌套水平RecyclerView橫向滑動不流暢問題
解決思路:循環(huán)查找父親View是否為ViewPager,如果是贷屎,則阻止父view攔截事件
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/*--------解決垂直RecyclerView嵌套水平RecyclerView橫向滑動不流暢問題 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;
// 保證子View能夠接收到Action_move事件
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);
// 這里是夠攔截的判斷依據(jù)是左右滑動龄减,讀者可根據(jù)自己的邏輯進行是否攔截
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;
}
/*--------解決垂直RecyclerView嵌套水平RecyclerView橫向滑動不流暢問題 end --------*/
return super.dispatchTouchEvent(ev);
}
完整代碼
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) {
/*--------解決垂直RecyclerView嵌套水平RecyclerView橫向滑動不流暢問題 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;
// 保證子View能夠接收到Action_move事件
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);
// 這里是夠攔截的判斷依據(jù)是左右滑動,讀者可根據(jù)自己的邏輯進行是否攔截
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;
}
/*--------解決垂直RecyclerView嵌套水平RecyclerView橫向滑動不流暢問題 end --------*/
/*---解決ViewPager嵌套垂直RecyclerView嵌套水平RecyclerView橫向滑動到底后不滑動ViewPager start ---*/
ViewParent parent =this;
while(!((parent = parent.getParent()) instanceof ViewPager));// 循環(huán)查找viewPager
parent.requestDisallowInterceptTouchEvent(true);
/*---解決ViewPager嵌套垂直RecyclerView嵌套水平RecyclerView橫向滑動到底后不滑動ViewPager start ---*/
return super.dispatchTouchEvent(ev);
}
}