java - 由于 recyclerView 导致 Activity/fragment 滞后
问题描述
当 recyclerView 在片段/活动中可见时,加载活动/片段的延迟。如果活动没有显示(可见性:消失)回收者查看滞后消失了......
private void setRecentHisRecycler(List<OrderList> popularAdsList) {
ordersAdapter = new OrdersAdapter(getContext(), sagaPopularAdsList, getActivity());
binding.orderHisRecentRecycler.setHasFixedSize(true);
binding.orderHisRecentRecycler.setNestedScrollingEnabled(false);
binding.orderHisRecentRecycler.setScrollContainer(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
binding.orderHisRecentRecycler.setLayoutManager(layoutManager);
binding.orderHisRecentRecycler.setAdapter(ordersAdapter);
}
这段代码没问题..
profileViewModel.getRecentOrderItem().observe(getViewLifecycleOwner(), orderLists1 -> {
orderLists.clear();
orderLists.addAll(orderLists1);
ordersAdapter.notifyDataSetChanged();
binding.perviousOrderLayout.setVisibility(orderLists.size() <= 0 ? View.GONE : View.VISIBLE);
});
当我将 recuclerView ConstrainingLayout Visiblity 设置为可见时,片段加载时间增加 300 毫秒至 2 秒..
如果我过去
binding.perviousOrderLayout.setVisibility(orderLists.size() <= 0 ? View.GONE : View.VISIBLE);
在观察者开始时,滞后消失了,但数据不可见。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:gravity="bottom"
android:orientation="vertical"
tools:context=".ui.ProfileFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/profileToolbarLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="5dp"
android:gravity="center"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="lollipop">
<TextView
android:id="@+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bitter_bold"
android:text="My Account"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/perviousOrderLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/yourOrdersTv">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/orderHisRecentRecycler"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hapticFeedbackEnabled="true"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:nestedScrollingEnabled="true"
tools:itemCount="1"
tools:listitem="@layout/orders_list" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
我解决了这个问题...使用多视图 RecyclerView 而不是 nestedScrollView
推荐阅读
- c++ - 数组必须用大括号括起来的初始化程序 c++ 初始化
- postman - VisualStudio2019 WebAPI 从 Postman 运行
- css - Next js 移动高度
- flutter - 尽管更新了提供程序,但 Flutter Hooks Riverpod 未更新小部件
- python - 如何突出显示包含特定对象的数据框中的所有单元格
- c++ - 没有默认构造函数的多重继承菱形问题
- javascript - 验证前面的文本输入是否与文本输入匹配
- sql - Oracle SQL 查询 - 包含子查询中每个元素的元素
- prolog - 如何将序言列表分成两个单独的列表?
- regex - 正则表达式中的文件夹排除