DialogFragment 中的 Recyclerview

Gun*_*lan 7 android android-recyclerview

我正在使用以下代码来创建 xml 布局资源。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="@dimen/dp48"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:fontFamily="@font/trebuchet"
        android:text="@string/select_a_folder"
        android:textColor="?attr/colorAccent"
        android:textSize="16sp"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_toTopOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewFolderList"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="350dp"
        android:maxHeight="350dp"
        app:layout_constraintBottom_toTopOf="@+id/imgViewClose2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imgViewClose2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/close"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:src="@drawable/ic_baseline_close_24dx"
        android:text="@string/cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

当我加载 recyclerview 中的一些项目时,我得到了预期的输出,

在此输入图像描述

但是,如果我加载近 50 个项目,recyclerview 会填充整个视图,因此我的标题和取消按钮丢失。

在此输入图像描述

如果我设置了layout_height="0dp"recyclerview,recyclerview就完全消失了,如下所示。

在此输入图像描述

我不想修复recyclerview的大小;如果我修复它,它会起作用,但它总是固定的,即使我只传递 2 个项目,那里也会有空白空间。

所以我希望recyclerview应该是wrap_content并且标题和按钮应该是可见的。

Gun*_*lan 13

这个简单的改变就让它发挥作用了。

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewFolderList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintBottom_toTopOf="@+id/imgViewClose2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView1" />
Run Code Online (Sandbox Code Playgroud)

感谢所有试图帮助我的人。

  • app:layout_constraintHeight_default="wrap" 修复了该问题 (5认同)
  • @AhmedAdelIsmail 看,我将“layout_height”从“wrap_content”更改为“0dp”,还删除了“maxHeight”和“layout_constraintHeight_max”属性。 (2认同)