Scrolling behaviour conflicts with a child RecyclerView and a parent Viewpager2

六眼飞鱼酱① 提交于 2020-06-27 12:54:05

问题


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

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.

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 :)


回答1:


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

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.

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>

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

Hope that helps!




回答2:


Put your recyclerview inside NestedScrollView and set below property on recyclerview and

android:nestedScrollingEnabled="false"

Try this or manage touch event on recyclerview



来源:https://stackoverflow.com/questions/58415985/scrolling-behaviour-conflicts-with-a-child-recyclerview-and-a-parent-viewpager2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!