控制ViewPager嵌套相关的canScroll方法

本文针对一个ViewPager内包含三个具有纵向RecyclerView的Fragment,且部分Item包含可横向滑动的相册RecyclerView的需求进行分析。问题在于,当相册RecyclerView滑动到边界时,继续滑动会导致ViewPager切换页面。解决方案是自定义ViewPager,在canScroll方法中进行判断,防止在边界时拦截事件,确保正确滑动体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


需求:

一个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做判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值