滚动行为与子级RecyclerView和父级Viewpager2冲突

编程入门 行业动态 更新时间:2024-10-10 13:13:47
本文介绍了滚动行为与子级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示例中提供了一种解决方案.他们在其中创建了一个名为 NestedScrollableHost ,您只需将其添加到项目中即可.然后,您可以将RecyclerView与其包装在一起,如下所示,以处理被拦截的触摸/滑动事件:

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冲突

本文发布于:2023-11-23 04:22:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:与子   冲突   RecyclerView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!