大致文件结构:
1.定制数据类型,定义一个实体类,新建Fruit.java
package com.example.listviewtest;
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 int getImageId() {
return imageId;
}
}
指定一个自定义的布局,在layout目录下新建fruit_item.xml
<?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="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"
android:layout_marginLeft="10dp"/>
</LinearLayout>
自定义适配器。现在要自定义一个继承自ArrayAdapter的适配器来填充数据。新建FruitAdapter.java
package com.example.listviewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class FruitAdapter extends ArrayAdapter<Fruit>{
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
//textViewResourceId为ListView子项布局的Id
resourceId = textViewResourceId;
}
//getView()方法在每个子项被滚动屏幕内的时候被调用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); //获取当前项的Fruit实例
//通过resourceId来加载我们自己定义的item的布局
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view; //返回我们自己定义的布局
}
}
修改MainActivity.java文件。
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化水果数据
initFruits();
FruitAdapter adapter = new FruitAdapter(this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for(int i=1;i<=10;i++) {
Fruit apple = new Fruit("Apple", R.mipmap.apple_pic);
fruitList.add(apple);
Fruit orange = new Fruit("Orange", R.mipmap.orange_pic);
fruitList.add(orange);
Fruit pear = new Fruit("Pear", R.mipmap.pear_pic);
fruitList.add(pear);
Fruit cherry = new Fruit("Cherry", R.mipmap.cherry_pic);
fruitList.add(cherry);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
最后效果: