ScrollView嵌套两个RecyclerView 导致滑动卡顿不流畅的原因

项目中在ScrollView内嵌套两个RecyclerView导致滑动卡顿。通过设置recyclerView.setNestedScrollingEnabled(false)或者自定义ScrollView来解决这个问题,以提升用户体验。
摘要由CSDN通过智能技术生成

       项目中展示两个RecyclerView ,所以在 ScrollView下嵌套了两个,完成后发现界面滑动不流畅,还会出现卡顿的情况,严重影响了用户的使用。网上查询资料,经测试目前有两种方法解决:

1、recyclerView.setNestedScrollingEnabled(false);

简单粗暴,在代码下加入这行代码。

2、自定义ScrollView

public class ScrollView extends ScrollView{
    private int downX;
    private int downY;
    private int mTouchSlop;
 public TopicScrollView(Context context) {
        super(context);
        mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop();
    }
    public TopicScrollView(Context context, AttributeSet attrs) {
      super(context, att
如果您想使用SmartRefreshLayout嵌套ScrollView嵌套多个RecyclerView,您需要在外部ScrollView和内部RecyclerView之间添加一个LinearLayout,然后将每个RecyclerView添加到LinearLayout中。接下来,您可以在LinearLayout中通过设置一个NestedScrollingChild来实现嵌套滚动。 以下是代码示例: ``` <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </androidx.core.widget.NestedScrollView> ``` 然后在代码中,您需要在RecyclerView上设置NestedScrollingEnabled为false,以防止RecyclerView自己处理滚动事件,而是让NestedScrollView来处理。 ``` recycler1.setNestedScrollingEnabled(false); recycler2.setNestedScrollingEnabled(false); recycler3.setNestedScrollingEnabled(false); ``` 最后,您可以在NestedScrollView上设置SmartRefreshLayout的布局参数,如下所示: ``` <com.scwang.smart.refresh.layout.SmartRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:enableLoadMore="true" app:enableRefresh="true"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </androidx.core.widget.NestedScrollView> </com.scwang.smart.refresh.layout.SmartRefreshLayout> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值