首页 > 解决方案 > 使用 ConstraintLayout 在 RecyclerView 中 Listview 不可滚动

问题描述

我有一个RecyclerView里面包含ListView。我必须使用,因为ConstraintLayout需要为. 但是当屏幕上的内容太多时,列表视图似乎无法滚动。TextViewListViewRecyclerView

recyclerview_layout.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:textColor="#0E1C2B"
    android:textSize="17sp"
    app:layout_constraintBottom_toTopOf="@+id/recyclerview_list"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.333" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:text="Date: "
    android:textColor="#0E1C2B"
    android:textSize="17sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.207"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/recyclerview_list"
    android:layout_width="260dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.43"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView4"
    app:layout_constraintVertical_bias="0.012" />

    </androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-constraintlayout

解决方案



*我自己的解决方案*
必须在 ListView 元素周围添加 `LinearLayout`。

那些带有星星的线条与原来的线条不同。

.
最终recyclerview_layout.xml:

<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">

---------other code are the same------------

<LinearLayout
    android:layout_width="300dp"
    **android:layout_height="wrap_content"**
    **android:orientation="vertical"**
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.47"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.011">

    <ListView
        android:id="@+id/recyclerview_list"
        **android:layout_width="match_parent"**
        **android:layout_height="wrap_content"**
        tools:layout_editor_absoluteX="104dp"
        tools:layout_editor_absoluteY="59dp" />
    
</LinearLayout>

推荐阅读