英文:
Lag in Activity/fragment due to recyclerView
问题
加载活动/片段时存在延迟,当RecyclerView在片段/活动中可见时。如果活动未显示(visibility:gone),则延迟消失...
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);
});
当我将RecyclerView的ConstraingLayout可见性设置为visible时,片段加载时间增加了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>
英文:
Lag in loading activity/fragment when the recyclerView is visible in fragment/activity. If the activity is not show (visibility:gone) the recyclerview the lag gone...
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);
}
no Problem with this code..
profileViewModel.getRecentOrderItem().observe(getViewLifecycleOwner(), orderLists1 -> {
orderLists.clear();
orderLists.addAll(orderLists1);
ordersAdapter.notifyDataSetChanged();
binding.perviousOrderLayout.setVisibility(orderLists.size() <= 0 ? View.GONE : View.VISIBLE);
});
when I set recuclerView ConstraingLayout Visiblity to visible the fragment loading time increase 300ms to 2 seconds..
if I past the
binding.perviousOrderLayout.setVisibility(orderLists.size() <= 0 ? View.GONE : View.VISIBLE);
in start of the observer the lag gone but the data not 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>
答案1
得分: 0
我修复这个问题... 使用多视图 RecyclerView 替代嵌套的 ScrollView
英文:
I fix this... Use Multi view RecyclerView instead of nestedScrollView
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论