如何在cardView android中添加listView? [英] How to add listView inside cardView android?

查看:557
本文介绍了如何在cardView android中添加listView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的任何错误.我是初学者. 任何人都可以解释一下如何在android的cardview布局内创建一个listview. android 6.0中的示例设置应用程序
我想用每个cardview布局中的listview项目创建一个可滚动的cardview布局. 我在网上搜索了足够的内容,似乎没有任何帮助. 如果您有任何解决方案,这对我会有所帮助.

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.

推荐答案

做到这一点的最佳方法是使用带有垂直LinearLayoutManagerRecyclerView(与ListView相同,但效果更好)性能)和CardView中的固定大小.您的CardView的xml如下所示:

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>

,然后以编程方式将RecyclerView上的固定大小设置为true,设置LayoutManager并创建一个自定义RecyclerView.Adapter来填充RecyclerView的行:

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

这篇关于如何在cardView android中添加listView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆