Recyclerview 嵌套滑动冲突和非嵌套去除滑动效果的完美解决方案

实际开发过程中,经常会使用Recyclerview嵌套 RecyclerView 或者 ScrollView 中嵌套Recyclerview。然而通过这种嵌套方式会导致 Recyclerview 显示不全、滑动卡顿等问题,下面我们就来提出解决以上问题的方案。

嵌套滑动解决方案

第一种方案 : 将Recyclerview 放置于 LinearLayout 或 RelateveLayout 中

		  <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:overScrollMode="never"
                    android:scrollbars="none" />

            </LinearLayout>

第二种方案 :用 NestedScrollView 来替换 ScrollView

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
            
    </LinearLayout>
    
</android.support.v4.widget.NestedScrollView>

但是第二种方案还是会出现滑动卡顿问题,这是只需要禁止 RecyclerView 的滑动即可,通过在 xml 中给 RecyclerView 添加 android:nestedScrollingEnabled=“false” 或者 直接在代码中设置 recyclerView.setNestedScrollingEnabled(false); 也可结合方案三使用

第三种方案:重写LinearLayoutManager的 canScrollVertically 方法从而禁止滑动 ,代码如下

recyclerView.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值