如果页面界面的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>