我在约束布局中有一个recyclerView,但我无法使其滚动,列表仅在屏幕下方继续,没有滚动的可能性。如果我将布局转换为相对布局,则滚动效果很好。
我如何使其滚动?
以下XML显示了我的布局,回收者视图在底部。布局在屏幕顶部具有图像和说明。此屏幕设置占用屏幕的30%。然后是分隔符和回收站视图,该视图应占据屏幕的其余部分,并且无法滚动
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gradient_top">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageViewLock"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/textViewPermissionsTitle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_phone_lock"/>
<TextView
android:id="@+id/textViewPermissionsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="center"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="@string/allow_permissions"
android:textColor="@color/white"
android:textSize="@dimen/description_text_size"
app:layout_constraintBottom_toTopOf="@+id/guideline1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<android.support.constraint.Guideline
android:id="@+id/guideline1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"/>
<View
android:id="@+id/viewSeparator"
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:layout_marginTop="10dp"
android:background="@color/bright_blue"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline1"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewPermissions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="1dp"
android:scrollbarThumbVertical="@color/white"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />
</android.support.constraint.ConstraintLayout>
要滚动RecyclerView
,必须满足以下两个条件之一:
RecyclerView
的高度小于其所有项目的高度RecyclerView
在滚动父内部[ConstraintLayout
不是滚动父级,因此我们必须确保RecyclerView
太小”,这将导致它让用户滚动其子级。
最简单的方法是给RecyclerView
一个定义的高度,像这样:
android:layout_height="200dp"
当然,这只有在您提前知道您想要RecyclerView
的大小时才有效,通常不是这种情况。
更好的解决方案是使用约束来定义RecyclerView
的高度。第一步是将其设置为0dp
的高度,可以将其视为“匹配约束”。换句话说,您要告诉系统使RecyclerView
尽可能高,以满足其顶部和底部约束。
接下来,您必须定义顶部和底部约束。最简单的方法是将RecyclerView
的顶部限制在父级的顶部,将其底部限制在父级的底部。可能看起来像这样:
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
或者,您可以相对于其他某些视图放置RecyclerView
。可能看起来像这样:
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/viewAboveMe"
app:layout_constraintBottom_toTopOf="@+id/viewBelowMe"
只要您将0dp
的高度与顶部和底部约束都结合在一起(并且只要RecyclerView
实际上小于其内容),这将允许RecyclerView
根据需要滚动。
您的RecyclerView
使用其高度为wrap_content
。如果要滚动,则必须提供固定的高度,或者在ConstraintLayout
内使用0dp
作为其高度,并为其提供app:layout_constraintBottom_xxx
属性。