1 实验名称
RecyclerView瀑布流水果列表
2 实验目的
掌握RecyclerView控件的实现方法和基本应用
3 实验源代码
布局文件代码:
activity_main:
java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
fruit_item:
java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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"
/>
</LinearLayout>
Java代码:
MainActivity .java:
java
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitLists = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化水果列表数据
initFruits();
//创建循环列表对象
RecyclerView recycler_view = findViewById(R.id.recycler_view);
//创建一个线性布局管理器对象
//LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//设置线性布局管理器的管理方向为水平管理
//linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//设置循环列表的布局管理器
//创建一个3列垂直的瀑布流布局管理器对象
StaggeredGridLayoutManager staggeredGridLayoutManager =
new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recycler_view.setLayoutManager(staggeredGridLayoutManager);
//创建列表数据适配器对象并把数据源关联上
FruitAdapter fruitAdapter = new FruitAdapter(fruitLists);
//让循环列表使用适配器对象
recycler_view.setAdapter(fruitAdapter);
}
private void initFruits(){
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);
fruitLists.add(apple);
Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);
fruitLists.add(banana);
Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);
fruitLists.add(orange);
Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);
fruitLists.add(watermelon);
Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);
fruitLists.add(pear);
Fruit grape = new Fruit(getRandomLengthName("Grape"), R.drawable.grape_pic);
fruitLists.add(grape);
Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
fruitLists.add(pineapple);
Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
fruitLists.add(strawberry);
Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
fruitLists.add(cherry);
Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
fruitLists.add(mango);
}
}
private String getRandomLengthName(String name){
Random random = new Random();
int length = random.nextInt(20);
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<length;i++){
stringBuilder.append(name);
}
return stringBuilder.toString();
}
}
FruitAdapter.java:
java
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> fruitLists = null;//子项水果数据源列表
public FruitAdapter(List<Fruit> fruitLists) {
this.fruitLists = fruitLists;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//得到子项布局的视图
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fruit_item,parent,false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Fruit fruit = fruitLists.get(position);//根据位置得到一个水果对象
//将水果图片放到子项视图持有器的ImageView中
holder.fruit_image.setImageResource(fruit.getImageId());
//将水果名字放到子项视图持有器的TextView中
holder.fruit_name.setText(fruit.getName());
}
@Override
public int getItemCount() {
return fruitLists.size();
}
//列表子项的视图持有器
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruit_image;
TextView fruit_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
fruit_image = itemView.findViewById(R.id.fruit_image);
fruit_name = itemView.findViewById(R.id.fruit_name);
}
}
}
Fruit.java:
java
//自定义数据源类
public class Fruit {
private String name;
private int imageId;//水果图片ID号
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
4 实验运行结果图
5 实验总结
先写布局文件,两个布局文件,分别是activity_main.xml布局文件和fruit_item.xml布局文件。在activity_main.xml布局文件中,设置了一个瀑布流列表;而fruit_item.xml布局文件中在列表中插入图片和文本框。
写完布局文件开始写Java代码,初始化水果列表数据,创建循环列表对象,创建一个三列垂直的瀑布流布局管理器对象,创建列表数据适配器对象并把数据源关联上,让循环列表适用适配器对象,将水果图片和名字放到子项视图持有器中。