一、ListView的简单用法
首先创建一个项目:ListViewDemo
在xml文件中加入一个ListView,接下来在MainActivity中
首先我们应该准备ListView要显示的数据,但是数组中的数据无法直接传给ListView,这时我们就要借助适配器。Android中提供了很多适配器种类,这里用的ArrayAdapter,它通过泛型来指定要适配的数据类型,,然后再构造函数中把要适配的数据加入即可,这里的适配器需要三个参数(上下文,子布局,数据);这里用的子布局是android源生的布局,最后调用ListView的setAdapter()方法,将适配器传进去。
简单总结一下,简单的ListView大致有四步:
1) 准备数据 2)找到ListView的id 3)创建一个适配器ArrayAdapter三个参数(上下文,子布局,数据) 4)调用ListView的setAdapter()方法,将适配器传进去
程序运行截图 :
二、定制ListView的界面
只显示一段文本的ListView太单调了,现在我们准备好一些图片,分别对应图片上的水果名字。
首先我们需要定义一个实体类,作为ListView适配器的适配类型,新建类Fruit:
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;
}
}
Fruit类中只有两个字段,name代表水果的名字,imageId代表水果的照片的ID,然后需要为我们自己的ListView写个自定也的布局,在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"
android:orientation="horizontal" >
<ImageView
android:id="@+id/fruit_image"
android:layout_width="60dp"
android:layout_height="60dp/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"/>
</LinearLayout>
这个布局中定义了一个显示水果的名字的TextView和显示水果照片的ImageView,接下来我们需要创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类 ,新建类FruitAdapter:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId ;
/**
*
* @param context 上下文
* @param textViewResourceId ListView子项布局id
* @param objects 数据
*/
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
public View getView(int position ,View convertView ,ViewGroup parent){
//获取当前项Fruit的实例
Fruit fruit = getItem(position);
//给子项加载我们的布局
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
//找到相对应的ImageView和TextView实例
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;
}
}
FruitAdapter重写了父类的一组构造函数,将上下文、ListView子项布局和数据都传递进来。又重写了getView()方法,这个方法在每个子项被滚动到屏幕的时候都会被调用,在getView()方法方法中,首先通过getItem()获得当前项的Fruit的实例,然后使用LayoutInflater来为这个子项加载我们传入的布局, LayoutInflater.from(getContext())是获取LayoutInflater实例,接着再调用View的findViewById()方法,得到想对应的ImageView和TextView的实例,并设置图片和文字,最后将布局返回。
修改MainActivity的代码:
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);
initFruits();//初始化数据
ListView listView = (ListView) findViewById(R.id.list_view);
FruitAdapter adapter = new FruitAdapter(this, R.layout.fruit_item, fruitList);
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("apple", R.drawable.apple_pic);
fruitList.add(apple);
//可以添加很多,这一就添加一个演示
}
}
这里initFruits()方法来初始化数据,程序运行显示为
虽然现在的界面也挺简单的,但是我们通过修改fruit_item.xml的内容,就可以修改成想要的界面了。
三、提升ListView的运行效率
目前我们的ListView的运行效率很低,因为在FruitAdapter的getView()方法中每次都将布局重新加载一遍,当ListView快速滚动的时候这就会成为性能的瓶颈,在getView()方法中有个convertView这个参数,这个参数用于将之前加载好的布局进行缓存,以后便于重用,修改FruitAdapter的代码
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId ;
/**
*
* @param context 上下文
* @param textViewResourceId ListView子项布局id
* @param objects 数据
*/
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
public View getView(int position ,View convertView ,ViewGroup parent){
//获取当前项Fruit的实例
Fruit fruit = getItem(position);
//给自项加载我们的布局
View view ;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
//找到相对应的ImageView和TextView实例
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;
}
}
在getView()方法中进行判断,如果convertView为空,就是用LayoutInflater加载布局,如果不为空view = convertView,这样就不用重复加载布局,提高了Listview的运行效率; 虽然不用重复加载布局了,但是getView()方法中还是会调用View的FindViewById()方法来回去一次控件实例,我们借助一个ViewHolder来对这部分进行优化。
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId ;
/**
*
* @param context 上下文
* @param textViewResourceId ListView子项布局id
* @param objects 数据
*/
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
public View getView(int position ,View convertView ,ViewGroup parent){
//获取当前项Fruit的实例
Fruit fruit = getItem(position);
//给自项加载我们的布局
View view ;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
<strong>viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);</strong>
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
//设置相对应的图片和文字
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
我们新增了一个内部类ViewHolder,用于对控件的实例进行缓存,当convertView为空的时候,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View的setTag()方法,将ViewHolder对象存到View中,当convertView存在的时候,调用View的setTag()方法,把ViewHolder重新取出,这样所有控件的实例都缓存在ViewHolde中,就没有必要每次都通过findViewById()方法来获取控件实例了。
注:View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。