在活动中,我有TabLayout
和FrameLayout
用于加载片段。片段包含RecyclerView
。它只适用于第一次。但当我更改标签并返回上一个标签时,RecyclerView
不会滚动到正确。
主要活动
<android.support.v4.widget.NestedScrollView
android:fillViewport="true"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabMain"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<FrameLayout
android:id="@+id/containerMain"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
分段
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvMedia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
</LinearLayout>
recyclerView本身具有平滑的滚动功能,但是当我们需要将recyclelerView放在任何scrollView中时,它将无法像下面那样工作:
布局XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
对此的解决方案是我们需要使用nestedScrollView而不是如下所示的scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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>
</LinearLayout>
当我们使用nestedScrollView并将recyclerView放在nestedScrollView中时会出现问题,它会根据手势以各种速度滚动。滚动功能不会很流畅。
所以要解决这个问题,设置适配器后你要做的就是添加这行
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
这不是一个好的解决方案。将RecyclerView放置在NestedScrollView中,会导致RecyclerView适配器的所有元素被渲染,使用大量内存。在大多数具有较少内存的设备中,这可能会非常慢。
这种方法也可能导致禁用需要滚动,这将禁用视图回收,因此所有项目将立即初始化。在包含1000个项目的列表中。这将使应用程序滞后。如果您在用户向下滚动列表时加载固定数量的项目时使用分页,则可以避免使用它。
阅读有关分页的更多信息。
Pagination with RecyclerView – Etienne Lawlor – Medium
Android RecyclerView Pagination with Paging Library using MVVM ...