如何使用卡片创建水平滚动的 RecyclerView

Howto create horizontal scrolling RecyclerView with Cards

尝试设置一个水平滚动的 RecyclerView,它接受 CardView 并在屏幕上水平滚动它们。我试图通过创建一个 LinearLayout 并将 RecyclerView 方向设置为水平并将滚动条设置为水平来设置它。不幸的是,这不起作用,我仍然看到卡片填满整个垂直 space 并垂直滚动。不确定为什么会发生这种情况或如何解决它。

回收站视图:


Save New Duplicate & Edit Just Text
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewModel"
            type="com.cloudhospital.app.ui.viewmodel.HospitalsVM" />
    </data>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.fragment.GeneralHealthFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:orientation="horizontal"
            android:scrollbars="horizontal"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
             />

    </RelativeLayout>

</layout>

卡片视图:


Save New Duplicate & Edit Just Text
<androidx.cardview.widget.CardView 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/hospital_item_card"
    android:padding="8dp"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/White"
    app:cardCornerRadius="7.5dp">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toTopOf="@+id/name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.74" />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top|start"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        app:layout_constraintBottom_toTopOf="@+id/itemDescription"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/itemImage"
        tools:text="Some date" />

    <TextView
        android:id="@+id/itemDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|start"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/name" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

您应该以编程方式设置 LayoutManager

recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false )

要进行水平滚动,需要以编程方式将水平布局管理器设置为您的回收视图,如下所示。

 recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
 recyclerView.setAdapter(yourAdapter);