2

Forgive me for any mistakes. I'm a beginner. Can anyone please explain how to create a listview inside a cardview layout in android. Example settings app in android 6.0
enter image description here I wanna create a scrollable cardview layout with listview items in each cardview layout. I have searched enough online and nothing seems to help me. If you have any solution this it would be helpful for me.

Manokar
  • 239
  • 3
  • 12
Tore Binarflame
  • 186
  • 1
  • 4
  • 15

2 Answers2

9

The best way to do this is using a RecyclerView with a vertical LinearLayoutManager (which will look the the same as a ListView but with better performance) and a fixed size inside your CardView. The xml for your CardView will look something like this:

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

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

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

and then programmatically set fixed size on your RecyclerView to true, set the LayoutManager and create a custom RecyclerView.Adapter to fill the RecyclerView's rows:

RecyclerView recyclerView = parentView.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);

LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);

MyCustomAdapter adapter = new MyCustomAdapter(context, dataSet);
recyclerView.setAdapter(adapter);
Jeffalee
  • 1,080
  • 1
  • 7
  • 15
0

The cardView is a backgroud of the listView. So the item of this will like: enter image description here

Hope to help you!