1、导入依赖
根据:compileSdkVersion 31
implementation 'com.android.support:recyclerview-v7:31.1.0'
2、创建MainActivity,并在layout中的activity_main中布局RecycleView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3、创建页面的整体布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<!--显示图片-->
<ImageView
android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--显示名称-->
<TextView
android:id="@+id/img_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
</LinearLayout>
4.创建Fruit实体类
public class Fruit {
private String name;//水果名
private int ImgId;//水果图片的资源ID
public Fruit(String name, int imgId) {
this.name = name;
ImgId = imgId;
}
public String getName() {
return name;
}
public int getImgId() {
return ImgId;
}
}
5.创建RecycleView的适配器
package com.example.recycleviewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
//创建内部类接受控件
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView fruitImage;
TextView fruitName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
fruitImage = itemView.findViewById(R.id.img_view);
fruitName = itemView.findViewById(R.id.img_name);
}
}
/**
* 构造函数,把要展示的数据源传递进来,并赋值给全局变量mFruitList
* @param fruitlist
*/
public FruitAdapter(List<Fruit> fruitlist){
mFruitList = fruitlist;
}
/**
* 创建ViewHolder实例
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public FruitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载fruit_item布局
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
//将加载出来的布局传入构造函数
ViewHolder holder = new ViewHolder(view);
//返回ViewHoder的实例
return holder;
}
/**
* 对RecycleView子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull FruitAdapter.ViewHolder holder, int position) {
//通过position参数得到当前项的Fruit实例
Fruit fruit = mFruitList.get(position);
//讲数据设置到ViewHolder的ImageView和TextView
holder.fruitImage.setImageResource(fruit.getImgId());
holder.fruitName.setText(fruit.getName());
}
/**
* 告诉RecycleView一共有多少子项,直接返回数据源的长度
* @return
*/
@Override
public int getItemCount() {
return mFruitList.size();
}
}
6、开始使用RecycleView,修改MainActivity中的代码
package com.example.recycleviewtest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
initFruit();
//获取RecycleView的实例
RecyclerView recyclerView = findViewById(R.id.recycle_view);
//创建LinearLayout-Manager对象,并将其设置到RecycleView中
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//创建FruitAdapter的实例,并将水果数据传入到FruitAdapter的构造函数中
FruitAdapter adapter = new FruitAdapter(list);
recyclerView.setAdapter(adapter);
}
/*{"apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","cherry","Mongo",
"apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","cherry","Mongo"};*/
private void initFruit() {
for (int i=0;i<2;i++) {
Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
list.add(apple);
Fruit banana = new Fruit("Banana",R.drawable.banana_pic);
list.add(banana);
Fruit orange = new Fruit("Orange",R.drawable.banana_pic);
list.add(banana);
Fruit watermelon = new Fruit("Watermelon",R.drawable.banana_pic);
list.add(banana);
Fruit pear = new Fruit("Pear",R.drawable.banana_pic);
list.add(banana);
Fruit grape = new Fruit("Grape",R.drawable.banana_pic);
list.add(banana);
Fruit pineapple = new Fruit("Pineapple",R.drawable.banana_pic);
list.add(banana);
Fruit strawberry = new Fruit("Strawberry",R.drawable.banana_pic);
list.add(banana);
Fruit cherry = new Fruit("Cherry",R.drawable.banana_pic);
list.add(banana);
Fruit mongo = new Fruit("Mongo",R.drawable.banana_pic);
list.add(banana);
}
}
}
参考书籍:《第一行代码(第2版)》
|