滚动行为与子级RecyclerView和父级Viewpager2冲突 [英] Scrolling behaviour conflicts with a child RecyclerView and a parent Viewpager2

查看:456
本文介绍了滚动行为与子级RecyclerView和父级Viewpager2冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个垂直滚动的 ViewPager2 ,最后一个子项包含一个在同一方向滚动的 RecyclerView .

I have a vertical scrolling ViewPager2 and the last children contains a RecyclerView scrolling on the same direction.

这导致了一个冲突的行为,当我在包含此 RecyclerView 的页面时, ViewPager2 总是窃取滚动事件.在 RecyclerView 中进行滚动的唯一方法是,如果我滚动得真的很慢,或者如果我使它滚动得很快,就像滑动事件一样, ViewPager2 就会滚动并更改页面.

This is causing a conflicting behaviour, the ViewPager2 always steal the scroll event when I am at the page containing this RecyclerView. The only way to make the scroll inside the RecyclerView is if I scroll really slow, if I make it fast, like a swipe event the ViewPager2 gets scrolled and changes the page.

当前,我正在做一个修复程序,其中涉及当 ViewPager2 的页面更改为包含 RecyclerView ,但欢迎使用框架的通用解决方案:)

Currently I'm doing a fix that involves disabled the user interaction changing the flag isUserInputEnabled to false when the page of the ViewPager2 changes to this page that contains the RecyclerView, but a generic solution from the framework would be welcome :)

推荐答案

我遇到了与您类似的问题,并且通过官方

I had a similar issue to yours and I found the answer via the official documentation.

我不建议仅出于先前回答 Martin Macroncini 的原因而将RecyclerView放在NestedScrollView中.这将导致RecyclerView为每个单独的数据项创建ViewHolders,而无需考虑回收它们.那显然是非常低效的.

I would suggest NOT to place your RecyclerView within a NestedScrollView simply for the reason Martin Macroncini answered previously. This causes the RecyclerView to create ViewHolders for every single data item, with no regards to recycling them. That is obviously very inefficient.

相反,Google在其 ViewPager2示例中提供了一种解决方案.他们在其中创建了一个名为

Instead, Google provided a solution in their ViewPager2 samples where they created a generic wrapper class called NestedScrollableHost that you simply add to your project. Then you can wrap that RecyclerView with it, similar to below, to handle the intercepted touch/swipe events:

<NestedScrollableHost
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />

    </NestedScrollableHost>

如文档所述,这仅适用于ViewPager2的直接子代,在您的情况下应该可以正常工作.

As noted by the documentation, this only works for immediate children of the ViewPager2, which in your case should work just fine.

希望有帮助!

这篇关于滚动行为与子级RecyclerView和父级Viewpager2冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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