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

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切换就不会自动滑动的情况了。

发布了25 篇原创文章 · 获赞 46 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_20328181/article/details/80361453