一、步骤
1.初始化数据;
2.提供一个自己定制的Adapte;
3.找到ListView控件;
4.给ListView设置适配器
二、直接上代码
1.Fruit.java
package com.zhc.listviewtest;
public class Fruit {
private String name;//成员变量name
private int imageId;
//这个构造函数在MainActivity中的初始化水果数据initFruits的会被重写,参数重新赋值。
public Fruit(String name, int imageId) {
this.name = name;//将参数name赋值给成员变量name
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
2.fruit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
3.FruitAdappter.java
package com.zhc.listviewtest;
import java.util.List;
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;
public class FruitAdappter extends ArrayAdapter<Fruit> {
private int resourceId;
//重写父类ArrayAdapter的一组构造函数,用于将上下文、ListView子项布局的id和数据传递进来。
public FruitAdappter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;//textViewResourceId的值是由MainActivity里面的构造函数(new FruitAdappter())传过来的R.layout.fruit_item
}
//重写父类的getView方法,这个方法在每个子项被滚动到屏幕内的时候会被调用。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit=getItem(position);//获取当前项的Fruit实例
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;
}
}
4.MainActivity.java
package com.zhc.listviewtest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;
public class MainActivity extends Activity {
private List<Fruit> fruitList= new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.初始化水果数据
initFruits();
//2.提供一个自己定制的Adapter(跟android提供的适配器ArrayAdapter的原理是一样的)
FruitAdappter adapter = new FruitAdappter(MainActivity.this, R.layout.fruit_item, fruitList);
//3.找到ListView控件
ListView listView = (ListView) findViewById(R.id.list_view);
//4.给ListView设置适配器
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple=new Fruit("Apple",R.drawable.ic_launcher);
fruitList.add(apple);
Fruit banana=new Fruit("Banana",R.drawable.ic_launcher);
fruitList.add(banana);
Fruit orange=new Fruit("Orange",R.drawable.ic_launcher);
fruitList.add(orange);
Fruit watermelon=new Fruit("Watermelon",R.drawable.ic_launcher);
fruitList.add(watermelon);
Fruit pear=new Fruit("Pear",R.drawable.ic_launcher);
fruitList.add(pear);
Fruit grape=new Fruit("Grape",R.drawable.ic_launcher);
fruitList.add(grape);
Fruit pineapple=new Fruit("Pineapple",R.drawable.ic_launcher);
fruitList.add(pineapple);
Fruit strawberry=new Fruit("Strawberry",R.drawable.ic_launcher);
fruitList.add(strawberry);
Fruit cherry=new Fruit("Cherry",R.drawable.ic_launcher);
fruitList.add(cherry);
Fruit mango=new Fruit("Mango",R.drawable.ic_launcher);
fruitList.add(mango);
Fruit mango1=new Fruit("Mango1",R.drawable.ic_launcher);
fruitList.add(mango1);
Fruit mango2=new Fruit("Mango2",R.drawable.ic_launcher);
fruitList.add(mango2);
Fruit mango3=new Fruit("Mango3",R.drawable.ic_launcher);
fruitList.add(mango3);
Fruit mango4=new Fruit("Mango4",R.drawable.ic_launcher);
fruitList.add(mango4);
}
}
5.activity_main.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
/>
</RelativeLayout>
三、最终的效果图