我有一个具有以下结构的布局:
<?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的fragemnt。我将滚动所有布局,而不仅仅是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>
您的RecyclerView
的layout_height
设置为wrap_content
,它告诉它立即对所有项目进行充气,然后将其高度设置为项目的总高度,而无需滚动(即,滚动仅来自父级NestedScrollView
)。您想将其放置在NestedScrollView
外部并将其高度设置为match_parent
,这将告诉它填充可用空间。如果要使用RecyclerView
嵌套滚动来折叠工具栏,请参见this question以了解如何使用CoordinatorLayout
完成此操作。