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

本文介绍了解决ViewPager与Fragment内RecyclerView滑动冲突的方法。通过设置LinearLayout的descendantFocusability属性为blocksDescendants,阻止RecyclerView获取焦点,从而避免了在ViewPager切换时RecyclerView自动滑动的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值