Munez NS Munez NS - 6 months ago 39
Android Question

Can one use cardview for listview item and how

I would like to implement cardview in my app so that all listview items be cardviews. Is it simple as encapsulating listview item xml in cardview?

Answer

Yes. Underneath CardView is just a simple FrameLayout that you could just inflate into a ListView (or RecyclerView).

Here's an example:

<android.support.v7.widget.CardView
    android:id="@+id/ly_root"
    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="wrap_content"
    android:background="#FEFEFE"
    android:layout_margin="8dp"
    app:cardCornerRadius="4dp">

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


        <ImageView
            android:id="@+id/iv_cover"
            android:layout_width="wrap_content"
            android:layout_height="160dp"
            android:scaleType="centerCrop"
            android:src="@drawable/placeholder"/>

        ...

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

And here's that example live in action:

enter image description here

Of course you would need to implement a custom adapter to wire them up together. But this is as with any custom ListView item really. There's nothing special about it.