解决recycleView+scrollView导致recycleView缓存复用失效,和无论怎么滑动firstVIsiablePosition都是返回0的问题

如果页面界面的xml里包含两个recycleView和一个scrollView,root是一个scrollView包含一个垂直的LinearLayout,里面是一个横向的recycleView1,和一个纵向的recycleView2。(布局代码如下)

会导致两个问题:

1是recycleView的多级缓存复用机制失效,recycleView2会一直是渲染count个item,会导致性能差。

2如果代码逻辑要依赖firstVIsiablePosition和lastVisiablePosition,获取到的是一直是0和count-1,会引起一些莫名的bug

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/detailContainer"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll">

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

        <!-- 横向的RecyclerView1 -->
        <com.qmnl.pati.view.NestedRecyclerView
            android:id="@+id/recTopicRecyclerView"
            android:paddingLeft="@dimen/main_fragment_item_margin_left"
            android:paddingRight="@dimen/main_fragment_item_margin_left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"/>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/main_fragment_item_margin_left"
            android:paddingRight="@dimen/main_fragment_item_margin_left"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_weight="1">

            <!-- 纵向的RecyclerView1 -->
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/momentRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"/>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

解决方法是不要使用scrollView,比如改为(CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout)的组合,来实现整体滚动效果

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/detailContainer"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="0dp"
        app:expanded="false"
        android:background="@android:color/transparent"
        android:outlineProvider="none"
        android:stateListAnimator="@null">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp"
            android:outlineProvider="bounds"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <com.qmnl.pati.view.NestedRecyclerView
                android:id="@+id/recTopicRecyclerView"
                android:paddingLeft="@dimen/main_fragment_item_margin_left"
                android:paddingRight="@dimen/main_fragment_item_margin_left"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="beforeDescendants"/>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/main_fragment_item_margin_left"
        android:paddingRight="@dimen/main_fragment_item_margin_left"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_weight="1">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/momentRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"/>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值