ViewPager+Fragment切换时,RecyclerView向上自动滑动

xiaoxiao2021-02-28  33

ViewPager+Fragment在项目中经常会遇到,最近一个项目就是这种情况,ViewPager+Fragment,每一个Fragment又有RecyclerView滑动监听,但是在ViewPager切换页面时,Fragment的RecyclerView会自动向上滑动把上面的Banner遮盖住,试过一些方法,像是RecyclerView监听滑动状态,指定滑动位置等等,都没有效果,最后尝试禁止RecyclerView获取焦点,在父布局中加入 android:descendantFocusability="blocksDescendants"

使得RecyclerView不能获取到焦点,最后解决问题,附上源码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_hot" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>

这样就可以解决在Fragment中有RecyclerView时,ViewPager切换就不会自动滑动的情况了。

转载请注明原文地址: https://www.6miu.com/read-2619816.html

最新回复(0)