[英]Make recycler view to be scrollable vertically
我正在使用以下代碼來創建回收站視圖。 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 中的回收器視圖根本沒有滾動。 我錯過了什么? 約束布局是否支持相同的?
錯誤來自
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:itemCount="4"
將其替換為
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"
要使用 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.