1 实验名称
ListView水果列表
2 实验目的
掌握自定义ListView控件的实现方法
3 实验源代码
布局文件代码:
activity_main.xml:
<?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"android:orientation="vertical"tools:context=".MainActivity"><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/list_view"/>
</LinearLayout>
fruit_item.xml:
<?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"><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"/>
</LinearLayout>
Java代码:
MainActivaty.java:
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
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 ListView list_view = null;//初始化列表中的数据private String []dates = {"Apple", "Banana", "Orange", "Watermelon","Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango","Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape","Pineapple", "Strawberry", "Cherry", "Mango"};//创建一个水果列表数据源private List<Fruit> fruitList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//创建一个列表适配器//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,// android.R.layout.simple_list_item_1,dates);//list_view.findViewById(R.id.list_view);//list_view.setAdapter(arrayAdapter);initFruits();//将水果列表数据源中的数据生成//创建水果适配器对象FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,//列表子项的布局文件fruitList);//列表数据源//创建列表ListView listView = findViewById(R.id.list_view);listView.setAdapter(fruitAdapter);}private void initFruits(){for (int i = 0; i < 2; i++) {Fruit apple = new Fruit("Apple", R.drawable.apple_pic);fruitList.add(apple);Fruit banana = new Fruit("Banana", R.drawable.banana_pic);fruitList.add(banana);Fruit orange = new Fruit("Orange", R.drawable.orange_pic);fruitList.add(orange);Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);fruitList.add(watermelon);Fruit pear = new Fruit("Pear", R.drawable.pear_pic);fruitList.add(pear);Fruit grape = new Fruit("Grape", R.drawable.grape_pic);fruitList.add(grape);Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);fruitList.add(pineapple);Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);fruitList.add(strawberry);Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);fruitList.add(cherry);Fruit mango = new Fruit("Mango", R.drawable.mango_pic);fruitList.add(mango);}}}
Fruit.java:
package com.example.listviewtest;
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.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);resourceId = textViewResourceId;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = getItem(position); // 获取当前项的Fruit实例View view;ViewHolder viewHolder;if (convertView == null) {view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);viewHolder = new ViewHolder();viewHolder.fruitImage = (ImageView) view.findViewById (R.id.fruit_image);viewHolder.fruitName = (TextView) view.findViewById (R.id.fruit_name);view.setTag(viewHolder); // 将ViewHolder存储在View中} else {view = convertView;viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder}viewHolder.fruitImage.setImageResource(fruit.getImageId());viewHolder.fruitName.setText(fruit.getName());return view;}class ViewHolder {ImageView fruitImage;TextView fruitName;}}
4 实验运行结果图
5 总结
先写好布局文件,这个实验一共有量个布局文件。实验在activity_main.xml和fruit_item.xml布局文件中就是列表所罗列的水果;在activity_main.xml布局文件中,设置了一个列表;而fruit_item.xml布局文件中在列表每一行中插入图片和文本框。
在写Java代码过程中,初始化列表中的数据,创建一个水果列表数据源和一个列表适配器,将水果列表数据源中的数据生成;创建水果适配器对象,创建列表获取当前项的水果实例。