在RecyclerView中使用ConstraintLayout时,Listview无法滚动。

问题描述 投票:1回答:1

我有一个 RecyclerView 其中包含 ListView 里面。我必须使用 ConstraintLayout 因为我需要对准 TextViewListView 妥为 每行 RecyclerView. 但是当屏幕上的内容过多时,列表视图似乎无法滚动。

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>

下面是截图。 xml screenshot 我需要的最终结果如下图所示,两个箭头都是指一行的 RecyclerViewListView frame


编辑:解决方案 必须添加 <LinearLayout/> 左右 ListView 元素。

添加的星线是指那些有不同的

. 最终的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>

而我最后的结果截图供大家参考......(想必很少有人会和我有同样的问题hhhhh) 较长的分隔线是为 RecyclerView 项。较短的是为 ListView. final view

android android-constraintlayout
1个回答
1
投票

你想使用 RecyclerViewListView? 在这里,我已经添加了一个 RecyclerView 适当的约束。有时添加一个错误的约束会让你出现滚动错误。

如果你想在下面的设计中再加一个,请告诉我。

enter image description here

    <?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="match_parent">

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00:00"
        android:textColor="#0E1C2B"
        android:textSize="17sp"
        app:layout_constraintBottom_toBottomOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toTopOf="@+id/textView4" />

    <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_toStartOf="@+id/time"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_list"
        android:layout_width="260dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.