首页 > 解决方案 > RecyclerView 不显示 BottomSheetDialogFragment 内的所有项目

问题描述

我有带有输入字段和 RecyclerView 的 BottomSheetDialogFragment。ConstraintLayout 的高度设置为 match_parent,RecylcerView 的高度为 0dp。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <EditText
        android:id="@+id/et_enter_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/black"
        android:hint="Enter a city"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="Autofill,TextFields" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_result"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_enter_text"
        app:layout_constraintVertical_bias="0" />

</androidx.constraintlayout.widget.ConstraintLayout>

问题是 RecyclerView 在键盘打开时仅显示 9 个元素中的 6 个。

即使 android:windowSoftInputMode 设置为 adjustResize,键盘也会与 RecyclerView 重叠。期望的行为是将 RecyclerView 底部约束附加到键盘顶部。

看截图

这是底页的样式。

<style name="AppBottomSheetDialogTheme"
      parent="Theme.Design.Light.BottomSheetDialog">
      <item name="android:windowSoftInputMode">adjustResize</item>
</style>

标签: android

解决方案


请使用 LinearLayout 而不是约束布局,因为有时在约束布局内的回收器视图时会出现奇怪的行为


推荐阅读