How to add listView inside cardView android?

坚强是说给别人听的谎言 提交于 2020-01-02 22:38:37

问题


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.


回答1:


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);



回答2:


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

Hope to help you!



来源:https://stackoverflow.com/questions/37406901/how-to-add-listview-inside-cardview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!