对于我的应用,我在 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" />
androidx.constraintlayout.widget.ConstraintLayout
将解决您的问题,而无需任何复杂的实现
尝试做:
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 >
我只需要使用这个:
mMyRecyclerView.setNestedScrollingEnabled(false);
在我的 onCreateView()
方法中。
非常感谢!
您可以使用这种方式:
将此行添加到您的 recyclerView xml 文件中:
android:nestedScrollingEnabled="false"
或者在java代码中:
RecyclerView.setNestedScrollingEnabled(false);
希望这有帮助。
您可以尝试使用 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);
使用嵌套滚动视图而不是滚动视图解决了我的问题
<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>
我有类似的问题(我试图创建一个嵌套的 RecyclerViews 类似于 Google PlayStore 设计)。处理这个问题的最好方法是继承子类 RecyclerViews 并覆盖“onInterceptTouchEvent”和“onTouchEvent”方法。通过这种方式,您可以完全控制这些事件的行为方式并最终滚动。
用 NestedScrollView 替换 ScrollView 可以平滑滚动到底部。
所有答案的摘要(优点和缺点)
对于单个回收者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
科特林
为滚动视图下的每个 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" />
这里的每个答案都是一样的。我已经使用了每个人的建议。然后我发现 NestedScrollView 比 ScrollView 快所以
利用
<androidx.core.widget.NestedScrollView
代替
<ScrollView
并像往常一样使用它
recycleView.setNestedScrollingEnabled(false);
如果您在子视图中使用 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,这是您的提示!
卡提克
您可以将 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>
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);
或者您可以在回收站视图中设置 android:focusableInTouchMode="true"
<?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
简单将此行添加到您的 JAVA 类中
list.setNestedScrollingEnabled(false);
我自己也遇到过这个问题,滚动视图中有一个回收器视图,并且滚动似乎并不流畅。我的问题的原因是在我的要求不需要的回收器视图顶部有滚动视图。因此,在我删除了滚动视图并为回收站视图添加了 android:scrollbars="vertical"
后,滚动很流畅。
经过 3 天的研究,我解决了项目中的平滑滚动问题。
问题是在 item_user.xml
文件的背景中设置了 <layer-list>
可绘制对象,因此需要 GPU 时间进行渲染,这就是滚动不流畅的原因。所以请不要在适配器项的背景中使用复杂的 <layer-list>
drawable。
我的问题已通过上述解决方案解决,以下选项对我没有用
setNestedScrollingEnabled setHasFixedSize setItemViewCacheSize
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 的解决方法