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
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.
Asked
Active
Viewed 8,039 times
2

Manokar
- 239
- 3
- 12

Tore Binarflame
- 186
- 1
- 4
- 15
-
This can be done using scrollview and inside it put your components as per your requirements. – Nisarg May 24 '16 at 07:28
-
go through this link:-http://javapapers.com/android/android-cards-list-view/ – sushildlh May 24 '16 at 07:43
-
What is the problem to use ListView inside FrameLayout (CardView is actually FrameLayout)? – Petrov Dmitrii May 24 '16 at 07:59
2 Answers
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!