ViewPager 中的水平 RecyclerView 不滚动 [英] Horizontal RecyclerView inside ViewPager is not scrolling

查看:35
本文介绍了ViewPager 中的水平 RecyclerView 不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在复杂的层次结构中有一个水平 RecyclerView,看起来像这样 -

I have a horizontal RecyclerView inside a Complex hierarchy that looks like this -

<ViewPager id="+@id/first">
    <ViewPager id="+@id/second"> this viewpager is taking away the scroll event
        <SwipeToRefreshLayout>
            <RecyclerView> //this one is vertical
                <RecyclerView id="@id/rv1"> //this one is horizontal
                <RecyclerView id="@id/rv2"> //this one is working fine (different type)
            </RecyclerView>
        </SwipeToRefreshLayout>
    </ViewPager>
</ViewPager>

现在的问题是第二个 ViewPager 正在劫持 Horizo​​ntal RV 的滚动条.该垂直 RV 中有 2 种类型的水平 RV(RV1 和 RV2).但只有其中一个(RV1)面临这个问题.第二个 (RV2) 工作正常.此外,当我按住然后滚动工作正常.当 RV1 已经在滚动并且尚未稳定时,滚动也可以正常工作.我已经提到了其他关于设置 NestedScrolling false 的答案.似乎没有任何效果.

Now the problem is that the second ViewPager is hijacking the scroll from Horizontal RV. There are 2 type of Horizontal RV (RV1 and RV2) in that vertical RV. But only one of them (RV1) is facing this problem. The second one (RV2) is working properly. Also when I press and hold then the scrolling is working fine. And when RV1 is already scrolling and has not settle, then also scrolling works fine. I have referred to other answers talking about setting nestedScrolling false. Nothing seems to be working.

推荐答案

您可以通过重写 onInterceptTouchEvent 方法来实现:

You can achieve this by overriding onInterceptTouchEvent method:

 mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent event) {
                int action = event.getAction();
               
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        rv.getParent().requestDisallowInterceptTouchEvent(true);

                        break;
                
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView view, @NonNull MotionEvent event) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });

这篇关于ViewPager 中的水平 RecyclerView 不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆