目录
Fruit.java
FruitAdapter
MainActivity
activity_main.xml
fruit.xml
整体结构
Fruit.java
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;}
}
FruitAdapter
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 resource, List<Fruit> objects) {super(context, resource, objects);resourceId = resource;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = getItem(position); // 获取当前 fruit 实例// 加载布局文件if (convertView == null) {convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);}// 获取布局文件中的 ImageView 和 TextView 控件ImageView fruitImage = convertView.findViewById(R.id.fruit_image);TextView fruitName = convertView.findViewById(R.id.fruit_name);// 设置 ImageView 和 TextView 的值fruitImage.setImageResource(fruit.getImageId());fruitName.setText(fruit.getName());return convertView;}
}
MainActivity
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.ListView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private List<Fruit> fruitList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 隐藏系统自带标题栏ActionBar actionBar = getSupportActionBar();if (actionBar != null) {actionBar.hide();}// 初始化水果列表initFruits();// 创建自定义适配器FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit, fruitList);// 将适配器设置给 ListViewListView listView = findViewById(R.id.list_view);listView.setAdapter(adapter);//添加一个点击事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Fruit fruit = fruitList.get(position);Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();}});}private void initFruits() {// 添加水果数据到列表for (int i = 0; i < 2; i++) {Fruit m0 = new Fruit("苹果", R.drawable.m0);fruitList.add(m0);Fruit m1 = new Fruit("苹果", R.drawable.m1);fruitList.add(m1);Fruit m2 = new Fruit("苹果", R.drawable.m2);fruitList.add(m2);Fruit m3 = new Fruit("苹果", R.drawable.m3);fruitList.add(m3);Fruit m4 = new Fruit("苹果", R.drawable.m4);fruitList.add(m4);Fruit m5 = new Fruit("苹果", R.drawable.m5);fruitList.add(m5);Fruit m6 = new Fruit("苹果", R.drawable.m6);fruitList.add(m6);Fruit m7 = new Fruit("苹果", R.drawable.m7);fruitList.add(m7);Fruit m8 = new Fruit("苹果", R.drawable.m8);fruitList.add(m8);Fruit m9 = new Fruit("苹果", R.drawable.m9);fruitList.add(m9);}}
}
activity_main.xml ⬅ 跳转上篇文章
fruit.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"xmlns:tools="http://schemas.android.com/tools"tools:context=".Fruit"><ImageViewandroid:id="@+id/fruit_image"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/fruit_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:layout_marginLeft="10dp"/></LinearLayout>
最终效果
PNG资源素材请跳转 iconfont-阿里巴巴矢量图标库