如何在 cardView android 中添加 listView?

How to add listView inside cardView android?

如有错误请见谅。我是初学者。 谁能解释一下如何在 android 中的卡片视图布局中创建列表视图。 android 6.0
中的示例设置应用程序 我想在每个 cardview 布局中创建一个带有列表视图项目的可滚动 cardview 布局。 我已经在网上搜索了足够多,但似乎没有任何帮助。 如果您有任何解决方案,这将对我有所帮助。

cardView是listView的背景。所以这个项目会喜欢: enter image description here

希望能帮到你!

最好的方法是使用 RecyclerView 和垂直 LinearLayoutManager(看起来与 ListView 相同但性能更好)和固定大小在你的 CardView 里面。 CardView 的 xml 看起来像这样:

<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 的行:

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