Activity/fragment 由于 recyclerView 导致的延迟

huangapple go评论146阅读模式
英文:

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&lt;OrderList&gt; 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 -&gt; {
orderLists.clear();
orderLists.addAll(orderLists1);
ordersAdapter.notifyDataSetChanged();
binding.perviousOrderLayout.setVisibility(orderLists.size() &lt;= 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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;bottom&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.ui.ProfileFragment&quot;&gt;
&lt;androidx.core.widget.NestedScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/profileToolbarLayout&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;#FFFFFF&quot;
android:elevation=&quot;5dp&quot;
android:gravity=&quot;center&quot;
android:padding=&quot;16dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:targetApi=&quot;lollipop&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView26&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/bitter_bold&quot;
android:text=&quot;My Account&quot;
android:textAlignment=&quot;center&quot;
android:textAllCaps=&quot;true&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
tools:layout_editor_absoluteY=&quot;16dp&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/perviousOrderLayout&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;8dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/yourOrdersTv&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/orderHisRecentRecycler&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:hapticFeedbackEnabled=&quot;true&quot;
app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
android:nestedScrollingEnabled=&quot;true&quot;
tools:itemCount=&quot;1&quot;
tools:listitem=&quot;@layout/orders_list&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 0

我修复这个问题... 使用多视图 RecyclerView 替代嵌套的 ScrollView

英文:

I fix this... Use Multi view RecyclerView instead of nestedScrollView

huangapple
  • 本文由 发表于 2020年8月11日 22:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63360057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定