ChatGPT解决这个技术问题 Extra ChatGPT

ScrollView 内的 Recyclerview 滚动不顺畅

对于我的应用,我在 ScrollView 中使用了 RecyclerView,其中 RecyclerView 的高度基于使用 this library 的内容。滚动正常,但当我滚动 RecyclerView 时,它运行不顺畅。当我滚动 ScrollView 本身时,它正在平滑滚动。

我用来定义 RecyclerView 的代码:

LinearLayoutManager friendsLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), android.support.v7.widget.LinearLayoutManager.VERTICAL, false);
mFriendsListView.setLayoutManager(friendsLayoutManager);
mFriendsListView.addItemDecoration(new DividerItemDecoration(getActivity().getApplicationContext(), null));

ScrollView 中的 RecyclerView

<android.support.v7.widget.RecyclerView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/friendsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
这个解决方案对我有用:stackoverflow.com/a/32390370/7308789 谢谢
@tahaDev 在您的情况下到底什么不起作用,请详细说明。此外,似乎没有提供的解决方案适用于您的情况,是这样吗?
使用 androidx.constraintlayout.widget.ConstraintLayout 将解决您的问题,而无需任何复杂的实现

n
natario

尝试做:

RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);

作为替代方案,您可以使用支持设计库修改布局。我猜您当前的布局类似于:

<ScrollView >
   <LinearLayout >

       <View > <!-- upper content -->
       <RecyclerView > <!-- with custom layoutmanager -->

   </LinearLayout >
</ScrollView >

您可以将其修改为:

<CoordinatorLayout >

    <AppBarLayout >
        <CollapsingToolbarLayout >
             <!-- with your content, and layout_scrollFlags="scroll" -->
        </CollapsingToolbarLayout >
    </AppBarLayout >

    <RecyclerView > <!-- with standard layoutManager -->

</CoordinatorLayout >

然而,这是一条更长的路,如果您对自定义线性布局管理器没问题,那么只需禁用回收器视图上的嵌套滚动即可。

编辑(2016 年 4 月 3 日)

支持库的 v 23.2 版本现在在所有默认 LayoutManager 中包含工厂“包装内容”功能。我没有测试它,但你可能应该更喜欢它而不是你正在使用的那个库。

<ScrollView >
   <LinearLayout >

       <View > <!-- upper content -->
       <RecyclerView > <!-- with wrap_content -->

   </LinearLayout >
</ScrollView >

要添加到此答案:setNestedScrollingEnabled(false) 仅在我将 ScrollView 换成 NestedScrollView 时才有效。
对我来说,setNestedScrollingEnabled(false) 让我在 ScrollView 中的 RecyclerView 恢复了平滑滚动 - 谢谢!但我仍然不明白为什么会这样......?将嵌套滚动设置为 false 到底意味着什么?
请注意,android:nestedScrollingEnabled="false" 仅适用于 API 21+,但 v.setNestedScrollingEnabled(false) 适用于 <21。
为了将来参考,如果有人在 ScrollView 中遇到 发生在棉花糖/牛轧糖(API 23、24)设备上的 RecyclerView wrap_content 问题,请查看我在 stackoverflow.com/a/38995399/132121 的解决方法
我现在遇到的这个解决方案的一个缺点是 RecyclerView 不会在它的 onScrollListener 中接收事件。我需要它,因为我想在回收站中只有一定数量的物品时获取更多数据
A
AskNilesh

我只需要使用这个:

mMyRecyclerView.setNestedScrollingEnabled(false);

在我的 onCreateView() 方法中。

非常感谢!


i
iDeveloper

您可以使用这种方式:

将此行添加到您的 recyclerView xml 文件中:

android:nestedScrollingEnabled="false"

或者在java代码中:

RecyclerView.setNestedScrollingEnabled(false);

希望这有帮助。


需要 API 21+
V
Vishal Yadav

您可以尝试使用 XML 和编程两种方式。但是您可能面临的问题是(低于 API 21)使用 XML 将不起作用。所以最好在你的活动/片段中以编程方式设置它。

XML 代码:

<android.support.v7.widget.RecyclerView
      android:id="@+id/recycleView"
      android:layout_width="match_parent"
      android:visibility="gone"
      android:nestedScrollingEnabled="false"
      android:layout_height="wrap_content"
      android:layout_below="@+id/linearLayoutBottomText" /> 

以编程方式:

 recycleView = (RecyclerView) findViewById(R.id.recycleView);
 recycleView.setNestedScrollingEnabled(false);

S
Siddy Hacks

使用嵌套滚动视图而不是滚动视图解决了我的问题

<LinearLayout> <!--Main Layout -->
   <android.support.v4.widget.NestedScrollView>
     <LinearLayout > <!--Nested Scoll View enclosing Layout -->`

       <View > <!-- upper content --> 
       <RecyclerView >


     </LinearLayout > 
   </android.support.v4.widget.NestedScrollView>
</LinearLayout>

m
mahabudhi

我有类似的问题(我试图创建一个嵌套的 RecyclerViews 类似于 Google PlayStore 设计)。处理这个问题的最好方法是继承子类 RecyclerViews 并覆盖“onInterceptTouchEvent”和“onTouchEvent”方法。通过这种方式,您可以完全控制这些事件的行为方式并最终滚动。


m
minato

用 NestedScrollView 替换 ScrollView 可以平滑滚动到底部。


C
Community

所有答案的摘要(优点和缺点)

对于单个回收者view

您可以在 Coordinator 布局中使用它。

优势 - 它不会加载整个 recyclerview 项目。加载如此流畅。

缺点 - 你不能在 Coordinator 布局中加载两个 recyclerview - 它会产生滚动问题

参考 - https://stackoverflow.com/a/33143512/3879847

对于具有最少行的多个 recyclerview

您可以在 NestedScrollView 中加载

优点 - 它会平滑滚动

缺点 - 它会加载所有的 recyclerview 行,因此您的活动会延迟打开

参考 - https://stackoverflow.com/a/33143512/3879847

对于大行的多个recyclerview(超过100)

您必须使用recyclerview。

优势——滚动流畅,加载流畅

缺点 - 你需要编写更多的代码和逻辑

在多视图持有者的帮助下,将每个 recyclerview 加载到主 recyclerview 中

前任:

MainRecyclerview -ChildRecyclerview1 (ViewHolder1) -ChildRecyclerview2 (ViewHolder2) -ChildRecyclerview3 (ViewHolder3) -任何其他布局 (ViewHolder4)

multi-viewHolder 的参考 - https://stackoverflow.com/a/26245463/3879847


A
Anand Tripathi

科特林

为滚动视图下的每个 RecyclerView 将 isNestedScrollingEnabled 设置为 false

val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.isNestedScrollingEnabled = false

使用 XML 布局

<android.support.v7.widget.RecyclerView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/friendsList"
    android:layout_width="match_parent"
    android:nestedScrollingEnabled="false"
    android:layout_height="wrap_content" />

m
mr. groot

这里的每个答案都是一样的。我已经使用了每个人的建议。然后我发现 NestedScrollView 比 ScrollView 快所以

利用

<androidx.core.widget.NestedScrollView

代替

<ScrollView

并像往常一样使用它

recycleView.setNestedScrollingEnabled(false);

Y
Yyy

如果您在子视图中使用 VideoView 或重量级的小部件,请将高度为 wrap_content 的 RecyclerView 保持在高度为 match_parent 的 NestedScrollView 内,然后滚动将如您所愿完美地工作。

供参考,

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:nestedScrollingEnabled="false"
            android:layout_height="wrap_content"
            android:clipToPadding="false" />

</android.support.v4.widget.NestedScrollView>

感谢Micro,这是您的提示!

卡提克


它也为我工作。但我不明白它究竟是如何解决这个问题的?其背后的解决方案是什么?
k
khemraj kashyap

您可以将 ScrollView 用作父级,将 NestedScrollView 用作子级。像这样:-

       <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/CL1">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/eventRV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/CL1" />
    </androidx.core.widget.NestedScrollView>

M
Mohsinali

XML 代码:

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false" />

        </android.support.v4.widget.NestedScrollView>

在java代码中:

  recycleView = (RecyclerView) findViewById(R.id.recycleView);
     recycleView.setNestedScrollingEnabled(false);

B
Blue

或者您可以在回收站视图中设置 android:focusableInTouchMode="true"


H
Hitesh sapra
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintlayout_main"
                android:layout_width="match_parent"
                android:layout_height="@dimen/layout_width_height_fortyfive"
                android:layout_marginLeft="@dimen/padding_margin_sixteen"
                android:layout_marginRight="@dimen/padding_margin_sixteen"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <TextView
                    android:id="@+id/textview_settings"
                    style="@style/textviewHeaderMain"
                    android:gravity="start"
                    android:text="@string/app_name"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent" />

            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintlayout_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/padding_margin_zero"
                android:layout_marginTop="@dimen/padding_margin_zero"
                android:layout_marginEnd="@dimen/padding_margin_zero"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/constraintlayout_main">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="false"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent" />

            </android.support.constraint.ConstraintLayout>

        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

此代码适用于 ConstraintLayout android


N
Null Pointer Exception

简单将此行添加到您的 JAVA 类中

list.setNestedScrollingEnabled(false);

h
harishanth raveendren

我自己也遇到过这个问题,滚动视图中有一个回收器视图,并且滚动似乎并不流畅。我的问题的原因是在我的要求不需要的回收器视图顶部有滚动视图。因此,在我删除了滚动视图并为回收站视图添加了 android:scrollbars="vertical" 后,滚动很流畅。


S
Sanjayrajsinh

经过 3 天的研究,我解决了项目中的平滑滚动问题。

问题是在 item_user.xml 文件的背景中设置了 <layer-list> 可绘制对象,因此需要 GPU 时间进行渲染,这就是滚动不流畅的原因。所以请不要在适配器项的背景中使用复杂的 <layer-list> drawable。

我的问题已通过上述解决方案解决,以下选项对我没有用

setNestedScrollingEnabled setHasFixedSize setItemViewCacheSize