【问题标题】:RecyclerView loads all items at once, causing Application Not Responding errorRecyclerView 一次加载所有项目,导致 Application Not Responding 错误
【发布时间】:2023-03-21 23:55:01
【问题描述】:

我的布局结构如下:

<?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"
    tools:context=".Activites.Settings.ContactsActivity">

    <FrameLayout
        android:id="@+id/fl_contacts"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_mirriom_contacts">

    </FrameLayout>

    some other widgets

</androidx.constraintlayout.widget.ConstraintLayout>

在这个 frameLayout 中,我展示了一个包含 recyclerView 的片段。 我将滚动所有布局,而不仅仅是 recyclerView。 所以我实现了这个结构

<androidx.constraintlayout.widget.ConstraintLayout >
<NestedScrollView>
<androidx.constraintlayout.widget.ConstraintLayout >
 <FrameLayout
       android:layout_width="0dp"
        android:layout_height="wrap_content"
 />
 Other widgets
</androidx.constraintlayout.widget.ConstraintLayout >
</NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout >

在这种模式下滚动是正确的,但所有回收站项目一起加载并导致应用程序没有响应

这是我的片段

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Activites.Event.Fragments.MirriomContactsFrag">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_contact"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:listitem="@layout/item_user" />



</androidx.constraintlayout.widget.ConstraintLayout>

编辑

我的片段 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Activites.Event.Fragments.MirriomContactsFrag">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_contact"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:listitem="@layout/item_user" />



</androidx.constraintlayout.widget.ConstraintLayout>

主要活动:

<?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"
    tools:context=".Activites.Settings.ContactsActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">



    <FrameLayout
        android:id="@+id/fl_contacts"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_mirriom_contacts">

    </FrameLayout>

    <include
        android:id="@+id/app_bar"
        layout="@layout/search_appbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView27"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:onClick="deleteSyncedContactsItemClicked"
        android:padding="12dp"
        android:text="@string/delete_synced_contacts"
        android:clickable="true"
        android:background="@drawable/my_ripple"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar"
        android:focusable="true" />

    <TextView
        android:id="@+id/textView28"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="@string/sync_contacts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView27" />

    <TextView
        android:id="@+id/textView29"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/ic_share"
        android:drawablePadding="12dp"
        android:padding="12dp"
        android:onClick="inviteFriendsToMirorimItemClicked"
        android:text="@string/invite_friends_to_mirorim"
        android:textColor="@color/colorAccent"
        android:clickable="true"
        android:background="@drawable/my_ripple"
        app:drawableTint="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView28"
        android:focusable="true" />

    <com.example.mirriom.Widget.SwitchButton
        android:id="@+id/swt_sync_contacts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="12dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView28"
        app:layout_constraintEnd_toEndOf="@+id/textView28"
        app:layout_constraintTop_toBottomOf="@+id/textView27"
        app:sb_checked="true"
        app:sb_checked_color="#77d672"
        app:sb_checkline_width="56dp"
        app:sb_show_indicator="false"
        app:sb_uncheckcircle_width="26dp" />

    <TextView
        android:id="@+id/tv_mirriom_contacts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:onClick="mirriomContactItemClicked"
        android:padding="@dimen/text_clickable_padding"
        android:text="@string/mirriom"
        android:textColor="@color/influencer_text_selection"
        android:textSize="@dimen/default_text_size"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/recycler_contact"
        app:layout_constraintEnd_toStartOf="@+id/tv_all_contacts"
        app:layout_constraintTop_toBottomOf="@+id/textView29" />

    <TextView
        android:id="@+id/tv_all_contacts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12dp"
        android:onClick="AllContactItemClicked"
        android:padding="@dimen/text_clickable_padding"
        android:text="@string/all"
        android:textColor="@color/influencer_text_selection"
        android:textSize="@dimen/default_text_size"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/recycler_contact"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView29" />

    <TextView
        android:id="@+id/textView30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:text="@string/contacts"
        android:textColor="@color/text_color"
        android:textSize="@dimen/default_text_size"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/tv_mirriom_contacts"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv_mirriom_contacts" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-recyclerview android-scrollview


    【解决方案1】:

    您的RecyclerViewlayout_height 设置为wrap_content,这告诉它一次充气所有项目,然后将其高度设置为不滚动的项目的总高度(即滚动仅来自父级NestedScrollView)。您想将其放在NestedScrollView 之外并将其高度设置为match_parent(或0dp,如果它在ConstraintLayout 中,并且具有适当大小的约束),这将告诉它填充可用空间。如果您想使用RecyclerView 进行嵌套滚动以实现折叠工具栏等功能,请参阅this question 了解如何使用CoordinatorLayout 来完成此操作。

    【讨论】:

    • 在编辑中我添加了我的xmls ...请告诉我该怎么做才能解决我的问题...设置recyclerView macthParent并将滚动行为设置为recycler没有解决问题
    • 你到底想做什么,所有的事情是如何嵌套的?您为什么要尝试将 RecylerView 放在 NestedScrollView 中(正如我所说,您不应该这样做)?您粘贴的“编辑”XML 仍然具有 wrap_content,这是错误的 - 对于这种特殊情况,您可能希望将其设置为 0dp 以匹配约束(我已将此添加到我的答案中)。
    • 想要实现的是,我不想滚动我的 RecyclerView,而是滚动整个页面,而不仅仅是滚动回收器。将 app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到我的 RecyclerView 对我没有帮助
    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2021-10-08
    • 1970-01-01
    • 2017-03-05
    • 2017-06-21
    相关资源
    最近更新 更多