本示例通过使用的ArrayList自定义Place对象作为数据集添加带有图像和名称的地点列表。
活动/片段的布局或仅在使用RecyclerView的位置必须包含RecyclerView。不需要ScrollView或特定的布局。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
你可以使用任何类或原始数据类型的模型,如int,String,float[]或CustomObject。RecyclerView将引用List此对象/基元中的一个。
当列表项引用不同的数据类型(如文本,数字,图像(如本例中的位置))时,通常最好使用自定义对象。
public class Place {
// 这些字段将显示在列表项中
private Bitmap image;
private String name;
// 典型的构造函数
public Place(Bitmap image, String name) {
this.image= image;
this.name= name;
}
// 吸气剂
public Bitmap getImage() {
return image;
}
public String getName() {
return name;
}
}您必须指定将用于每个列表项的xml布局文件。在此示例中,将anImageView用于图像,将aTextView用于名称。该LinearLayout位置ImageView在左侧,TextView在图像的权利。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="8dp"> <ImageView android:id="@+id/image" android:layout_width="36dp" android:layout_height="36dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
接下来,您必须继承RecyclerView.Adapter和RecyclerView.ViewHolder。通常的类结构为:
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.ViewHolder> {
// ...
public class ViewHolder extendsRecyclerView.ViewHolder{
// ...
}
}首先,我们实现ViewHolder。它仅继承默认构造函数,并将所需的视图保存到某些字段中:
public class ViewHolder extendsRecyclerView.ViewHolder{
private ImageView imageView;
private TextView nameView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image);
nameView = (TextView) itemView.findViewById(R.id.name);
}
}适配器的构造函数设置使用的数据集:
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.ViewHolder> {
private List<Place> mPlaces;
public PlaceListAdapter(List<Place> contacts) {
mPlaces = contacts;
}
// ...
}要使用自定义列表项的布局,我们将覆盖方法onCreateViewHolder(...)。在此示例中,布局文件称为place_list_item.xml。
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.ViewHolder> {
// ...
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.place_list_item,
parent,
false
);
return new ViewHolder(view);
}
// ...
}在中onBindViewHolder(...),我们实际上设置了视图的内容。我们可以通过List在给定位置找到所使用的模型,然后在ViewHolder的视图上设置图像和名称来获得模型。
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.ViewHolder> {
// ...
@Override
public void onBindViewHolder(PlaceListAdapter.ViewHolder viewHolder, int position) {
Place place = mPlaces.get(position);
viewHolder.nameView.setText(place.getName());
viewHolder.imageView.setImageBitmap(place.getImage());
}
// ...
}我们还需要实现getItemCount(),只需返回List的大小即可。
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.ViewHolder> {
// ...
@Override
public int getItemCount() {
return mPlaces.size();
}
// ...
}对于此示例,我们将生成一些随机位置。
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
List<Place> places = randomPlaces(5);
// ...
}
private List<Place> randomPlaces(int amount) {
List<Place> places = new ArrayList<>();
for (int i = 0; i < amount; i++) {
places.add(new Place(
BitmapFactory.decodeResource(getResources(), Math.random() > 0.5 ?
R.drawable.ic_account_grey600_36dp :
R.drawable.ic_android_grey600_36dp
),
"Place #" + (int) (Math.random() * 1000)
));
}
return places;
}RecyclerView用适配器连接非常容易。您必须将LinearLayoutManageras设置为布局管理器才能实现列表布局。
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setAdapter(new PlaceListAdapter(places));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}