需求:
一个ViewPager中包含三个Fragment;
每个fragment中都是一个RecyclerView的纵向列表;
列表中有的Item为可横向滑动的类似相册浏览的RecyclerView.
要求:在横向滑动相册浏览的RecyclerView时,Viewpager不会随着一起滑动.
分析:
直接写代码是不会存在什么滑动冲突的,但是有个问题是当横向的相册RecyclerView滑动到最左(或最右)时,再向左(或者右)滑动,viewpager就会拦截滑动事件,导致viewpager开始向左边的Item滑动.
既然ViewPager会拦截事件,看onIntercepterTouchEvent方法的代码可以知道,在MotionEvent.ACTION_MOVE中会调用
if (dx != 0 && !isGutterDrag(mLastMotionX, dx)
&& canScroll(this, false, (int) dx, (int) x, (int) y)) {
// Nested view has scrollable area under this point. Let it be handled there.
mLastMotionX = x;
mLastMotionY = y;
mIsUnableToDrag = true;
return false;
}
其中canScroll方法,通过名称,就能猜到是用来判断子View是否能滑动
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight()
&& y + scrollY >= child.getTop() && y + scrollY < child.getBottom()
&& canScroll(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && ViewCompat.canScrollHorizontally(v, -dx);
}
用递归可以找到底层的View,代码没细看也能知道肯定是RecyclerView在滑动到最左或最右时,canScroll方法会饭后false,导致最后的结果为ViewPager拦截了事件.
解决方法:
自定义ViewPager
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
return super.canScroll(v, checkV, dx, x, y)||(v instanceof MyCustomRecyclerView)||(v instanceof HorizontalScrollView);
}
如果触摸的控件是我自定义的控件或者是其他有这种需求的控件,可以在canScroll做判断