繁体   English   中英

由于 recyclerView 导致 Activity/fragment 滞后

[英]Lag in Activity/fragment due to recyclerView

当 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM