ConstraintLayout中的android RecylerView不会滚动

Ben*_*nyP 19 android android-scrollview android-recyclerview android-constraintlayout

我在约束布局中有一个recyclerView,我无法使其滚动,列表只是在屏幕下方继续,没有滚动可能性.如果我将布局转换为相对布局,滚动工作正常.

怎么才能让它滚动?

以下XML显示了我的布局,Recycler视图位于底部.布局在屏幕顶部有一个图像和描述.此屏幕设置占用屏幕的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>
Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 45

对于RecyclerView滚动,两件事情之一必须是真实的:

  • 将RecyclerView具有比其所有项目的高度较小
  • 该RecyclerView是一个滚动的家长里

ConstraintLayout不是滚动父级,所以我们必须确保它RecyclerView"太小",这将导致它让用户滚动其子级.

最简单的方法是给出RecyclerView一个定义的高度,如下所示:

android:layout_height="200dp"
Run Code Online (Sandbox Code Playgroud)

当然,这只有在您提前知道您希望自己RecyclerView有多大的情况下才有效,而通常情况并非如此.

更好的解决方案是使用约束来定义您的高度RecyclerView.第一步是给它一个高度0dp,可以认为是"匹配约束".换句话说,你告诉系统要RecyclerView达到它需要的高度,以满足它的顶部和底部约束.

接下来,您必须定义顶部和底部约束.最简单的方法是将RecyclerView顶部约束到父级的顶部,将底部约束到父级的底部.这可能看起来像这样:

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)

或者,您可以将RecyclerView相对位置定位到其他视图.这可能看起来像这样:

android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/viewAboveMe"
app:layout_constraintBottom_toTopOf="@+id/viewBelowMe"
Run Code Online (Sandbox Code Playgroud)

只要你结合的高度0dp与顶部和底部的限制(只要你RecyclerView实际上是比它的内容更小),这将使您的RecyclerView滚动根据需要.

原版的

你RecyclerView正在使用wrap_content它的高度.如果你想要它滚动,你必须提供一个固定的高度,或在里面ConstraintLayout,使用0dp它的高度,并给它一个app:layout_constraintBottom_xxx属性.

  • 我不知道为什么,但在某些情况下将 recyclerView 高度设置为 0dp 及其所有垂直约束不起作用。我设法通过将 ConstraintLayout 替换为 LinearLayout 并将父级和 recyclerview 本身的高度设置为 match_parent 来使其水平滚动 (3认同)

小智 9

有时会发生这种情况是因为没有限制回收器视图的底部。这导致回收者视图具有超出可见屏幕的无限高度。