CardView 中的单个 ListView

Single ListView inside a CardView

我有这样的屏幕要求:

但我无法让它像那样工作。我最终制作了这样的视图:

我正在使用 CardView。列表内容正在动态扩展中。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/lblProductName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="Product Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblProductQuantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="99"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

如何使我的屏幕看起来与所需屏幕相同?

尝试在 xml 中使用下面的代码,将您的 ListView 放入 CardView 中;

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="@color/color"
        android:id="@+id/cv1"
        card_view:cardElevation="3.5dp"
        card_view:cardBackgroundColor="#fff"
        card_view:cardCornerRadius="3dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

           <ListView
            android:id="@+id/study_level_list"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

在卡片视图中添加您的 Recyclerview

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/color"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3.5dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recylerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</android.support.v7.widget.CardView>

并按如下方式更改您的适配器布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/lblProductName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="Product Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblProductQuantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="99"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>


</RelativeLayout>

将您的列表视图放入卡片视图

<android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            card_view:cardCornerRadius="2dp"
            card_view:contentPadding="10dp">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />
    </android.support.v7.widget.CardView>