项目名:RecyclerViewTest
作用:展示水果
RecyclerView类来自于谷歌支持库,要使用它,首先要添加RecyclerView依赖库。
创建一个Fruit类
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; } public int getImageId() { return imageId; } }
RecyclerView子项布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" /> </LinearLayout>
为RecyclerView准备一个适配器,新建FruitAdapter类