Fragment中嵌套使用Card View布局
先看效果:
Fragment的布局文件很简单,就一个RecyclerView布局
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
然后自己定义一个类(名字随便取的)
public class Fruit {
private String name;
private int imageId;
public Fruit (String name, int imageId){
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
int getImageId() {
return imageId;
}
}
再写一个布局文件
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<Im

这篇博客介绍了如何在Fragment中嵌套使用Card View布局,通过一个RecyclerView展示多个CardView。内容包括Fragment布局文件、自定义类、布局文件的设计,以及适配器的实现。特别提到ViewHolder的作用和提高性能的原理。在初始化视图时,应注意操作必须在onCreateView()中进行,并给出了使用GridLayoutManager的注意事项。同时,文章也提到了Fragment中不能直接使用findViewById()的解决方法,强调了Fragment生命周期的重要性。
最低0.47元/天 解锁文章
1352

被折叠的 条评论
为什么被折叠?



