gpt4 book ai didi

android - fragment 中的 Recyclerview 位于 nestedscrollview 滚动问题内

转载 作者:行者123 更新时间:2023-11-29 23:19:27 24 4
gpt4 key购买 nike

在 Activity 中,我有 TabLayoutFrameLayout 用于加载 fragment 。 fragment 包含 RecyclerView。它只适用于第一次。但是当我更改选项卡并返回到上一个选项卡时,RecyclerView 没有完全滚动。

Main Activity

<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>

Fragment

<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>

最佳答案

The recyclerView has a smooth scrolling by itself but when we need to put recyclerView within any scrollView it will not work like the below:

Layout 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>

The solution for this is we need to used nestedScrollView instead of scrollview like the below

<?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>

The problem occurs when we use nestedScrollView and put recyclerView inside nestedScrollView is, it scrolls in various speed depending on gesture. The scrolling feature will not be smooth.

So to fix this issue all you have to do after setting your adapter is to add this line ViewCompat.setNestedScrollingEnabled(recyclerView, false);

这不是一个好的解决方案。将 RecyclerView 放置在 NestedScrollView 中,会导致渲染 RecyclerView 适配器的所有元素,这会占用大量内存。在大多数内存较少的设备中,这可能会很慢。

这种方法也可能导致禁用需要滚动,这将禁用 View 回收,因此所有项目将立即初始化。例如在包含 1000 个项目的列表中。这将使应用程序滞后。如果在用户向下滚动列表时加载固定数量的项目时使用分页,则可以避免这种情况。

Read more about pagination.

Pagination with RecyclerView – Etienne Lawlor – Medium

Android RecyclerView Pagination with Paging Library using MVVM ...

Paging library overview | Android Developers

关于android - fragment 中的 Recyclerview 位于 nestedscrollview 滚动问题内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54649509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com