在 NestedScrollView 中,Recycler View 加载大数据时非常慢
Recycler View loading very slow for large data when inside NestedScrollView
我在 NestedScrollView
中添加了 RecyclerView
。基本上我希望 RecyclerView 与其他视图一起滚动。我面临的问题是,对于一小部分数据,它工作正常,但是对于大量数据(200 个条目),每当我启动 activity 时,它会冻结大约 3-5 秒然后加载。我删除了 NestedScrollView
,它工作得很好,但它没有提供我想要的行为。
(额外的信息,我是从SQLite数据库加载数据。滚动没有问题,因为它很流畅。唯一的问题是activity卡住了一段时间)
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<... Some other Views ...>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
本案RecyclerView
里面NestedScrollView
.
RecyclerView
正在调用 onCreateViewHolder()
次等于您的数据大小。
如果数据有 200 个项目,它会冻结 onCreateViewHolder()
被调用 200 次。
正如 Nancy 所说,recyclerview.setNestedScrollingEnabled(false);将解决滚动卡住的问题。我也遇到过这类问题,并通过错误的 NestedScroll 解决了。
上面说的问题是因为RecyclerView作为NestedScrollView中的child或subChild,当你使用WRAP_CONTENT或MATCH_PARENT作为RecyclerView的高度时,它的高度是不定式的。
为我解决此问题的一个解决方案是将 RecyclerView 高度设置为固定大小。您可以将高度设置为 dp 值,或者如果您的要求需要垂直不定式 RecyclerView,则可以将其设置为与设备高度匹配的像素值。
这是在 kotlin 中设置 recyclerView 大小的片段
val params = recyclerView.layoutParams
params.apply {
width = context.resources.displayMetrics.widthPixels
height = context.resources.displayMetrics.heightPixels
}
recyclerView.layoutParams = params
我遇到了同样的问题!
解决方案是将 NestedScrollView 更改为 SwipeRefreshLayout。
为 enable/disable 添加此工具栏滚动:
ViewCompat.setNestedScrollingEnabled(recyclerView, true);
我在 NestedScrollView
中添加了 RecyclerView
。基本上我希望 RecyclerView 与其他视图一起滚动。我面临的问题是,对于一小部分数据,它工作正常,但是对于大量数据(200 个条目),每当我启动 activity 时,它会冻结大约 3-5 秒然后加载。我删除了 NestedScrollView
,它工作得很好,但它没有提供我想要的行为。
(额外的信息,我是从SQLite数据库加载数据。滚动没有问题,因为它很流畅。唯一的问题是activity卡住了一段时间)
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<... Some other Views ...>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
本案RecyclerView
里面NestedScrollView
.
RecyclerView
正在调用 onCreateViewHolder()
次等于您的数据大小。
如果数据有 200 个项目,它会冻结 onCreateViewHolder()
被调用 200 次。
正如 Nancy 所说,recyclerview.setNestedScrollingEnabled(false);将解决滚动卡住的问题。我也遇到过这类问题,并通过错误的 NestedScroll 解决了。
上面说的问题是因为RecyclerView作为NestedScrollView中的child或subChild,当你使用WRAP_CONTENT或MATCH_PARENT作为RecyclerView的高度时,它的高度是不定式的。
为我解决此问题的一个解决方案是将 RecyclerView 高度设置为固定大小。您可以将高度设置为 dp 值,或者如果您的要求需要垂直不定式 RecyclerView,则可以将其设置为与设备高度匹配的像素值。
这是在 kotlin 中设置 recyclerView 大小的片段
val params = recyclerView.layoutParams
params.apply {
width = context.resources.displayMetrics.widthPixels
height = context.resources.displayMetrics.heightPixels
}
recyclerView.layoutParams = params
我遇到了同样的问题! 解决方案是将 NestedScrollView 更改为 SwipeRefreshLayout。
为 enable/disable 添加此工具栏滚动:
ViewCompat.setNestedScrollingEnabled(recyclerView, true);