使回收器视图可垂直滚动

时间:2021-03-23 13:55:06

标签: java android user-interface android-recyclerview android-constraintlayout

我正在使用以下代码创建回收器视图。 recyclerview 有一个网格布局管理器,每行最多有 2 个项目。

 <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:background="@color/white"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:focusable="true">

    <ImageView
        android:id="@+id/backPress"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/hello"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title!"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/textGoing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Subtitle"
        android:textSize="@dimen/text_average"
        android:textStyle="bold"
        app:layout_constraintTop_toBottomOf="@+id/hello" />
    <com.google.android.material.textfield.TextInputLayout
        android:layout_margin="@dimen/dimen_20"
        app:layout_constraintTop_toBottomOf="@+id/textGoing"
        android:id="@+id/anchor_input"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:boxBackgroundColor="@android:color/white"
        android:background="@android:color/transparent" >

        <EditText
            android:id="@+id/anchor_edit_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        app:layout_constraintTop_toBottomOf="@+id/anchor_input"
        app:layout_constraintStart_toStartOf="@+id/anchor_input"
        android:id="@+id/anchor_hint"
        android:layout_below="@+id/anchor_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


    <Button
        android:id="@+id/add_cat_btn"
        app:layout_constraintTop_toBottomOf="@+id/anchor_hint"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_below="@+id/anchor_hint"
        android:layout_marginTop="@dimen/dimen_20"
        android:text="START"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"/>

    <TextView
        android:id="@+id/category_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/add_cat_btn"
        android:text="Description"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/category_rv"
        app:layout_constraintTop_toBottomOf="@+id/category_title"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        tools:itemCount="4"
        android:layout_margin="@dimen/dimen_20"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/move_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:tint="@android:color/white"
        app:layout_constraintEnd_toEndOf="@+id/category_rv"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

不幸的是,我嵌套在 ConstraintLayout 中的回收器视图根本没有滚动。我错过了什么?约束布局是否支持相同?

2 个答案:

答案 0 :(得分:1)

错误来自

tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:itemCount="4"

替换为

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"

答案 1 :(得分:1)

要解决 RecyclerView 与 ConstraintLayout 父项的滚动问题,请将 RecyclerView 高度从 wrap_content 更改为 0dp,并添加如下所示的顶部和底部约束:

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/category_rv"
   android:layout_width="0dp"
   android:layout_height="0dp"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/category_title"
   app:layout_constraintBottom_toBottomOf="parent"/>