简体   繁体   English

使回收站视图可垂直滚动

[英]Make recycler view to be scrollable vertically

I'm using the following code to create a recycler view.我正在使用以下代码来创建回收站视图。 The recyclerview has got a grid layout manager with an item count of up to 2 per row. 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>

Unfortunately, the recycler view that I have nested within the ConstraintLayout is not scrolling at all.不幸的是,我嵌套在 ConstraintLayout 中的回收器视图根本没有滚动。 What am I missing?我错过了什么? Does constraint layout support the same?约束布局是否支持相同的?

The error comes from错误来自

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

replace it by将其替换为

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

To solve RecyclerView scrolling issue with ConstraintLayout parent change your RecyclerView height from wrap_content to 0dp and add top and bottom constraints like below:要使用 ConstraintLayout 父级解决 RecyclerView 滚动问题,请将 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM