如何在cardView android中添加listView?

时间:2016-05-24 07:22:51

标签: android listview material-design android-cardview

原谅我有任何错误。我是初学者。 任何人都可以解释如何在android中的cardview布局内创建一个列表视图。 android 6.0中的示例设置应用程序 enter image description here 我想在每个cardview布局中创建一个可滚动的cardview布局和listview项目。 我在网上搜索得足够多,似乎没有任何帮助。 如果您有任何解决方案,这对我有帮助。

2 个答案:

答案 0 :(得分:8)

执行此操作的最佳方法是使用RecyclerView垂直LinearLayoutManager(看起来与ListView相同,但性能更佳)并且在您的内部使用固定大小CardViewCardView的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);

答案 1 :(得分:0)

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

希望能帮到你!