01
这里我们通过代码及注释来详细说明一下ListView的使用方法。
02
步骤:
03
1、定义一个实体类,作为ListView适配器的适配类型
04
class Color{
05
private String name;
06
private int imageId;
07
08
public Color(String name,int imageId){
09
this.name = name;
10
this.imageId = imageId;
11
}
12
13
public String getName(){
14
return name;
15
}
16
17
public int getImageId(){
18
return imageId;
19
}
20
}
21
2、在layout下创建目标item的xml文件,并设置布局。设置item中的imageView时注意imageView的大小及图片的缩放处理
22
3、创建一个继承自ArratAdapter的适配器(类),注意泛型指定的数据类型
23
class ColorAdapter extends ArrayAdapter<Color>{
24
25
private int resourceId;
26
public ColorAdapter(Context context, int resource, List<Color> objects) { //将上下文、ListView子项布局的id和数据传递进来
27
super(context, resource, objects);
28
resourceId = resource; //保存ListView子项布局的id
29
}
30
@Override
31
public View getView(int position, View convertView, ViewGroup parent) { //此方法在每个子项被滚动到屏幕内的时候被调用
32
//return super.getView(position, convertView, parent);
33
//View view = LayoutInflater.from(getContext()).inflate(resourceId, null); //LayoutInflater来为这个子项加载我们传入的布局
34
Color color = getItem(position); //获取要显示在view上的数据
35
View view;
36
ViewHolder viewHolder;
37
if(convertView == null){ //避免了每次都加载一遍布局,当ListView快速滚动的时候就会出现瓶颈,通过此改动
38
view = LayoutInflater.from(getContext()).inflate(resourceId, null); //起始创建屏幕显示个数的view,当顶部的view彻底滑出屏幕的时候,
39
/*初始化viewHolder*/ //滑出的view就进入到Recycler中准备进行复用,
40
viewHolder = new ViewHolder(); //新滑进来的如果从Recycler中获取到了能复用的view,那么直接改写view中的数据即可
41
viewHolder.colorImage = (ImageView)view.findViewById(R.id.iv);
42
viewHolder.colorName = (TxetView)view.findViewById(R.id.tv);
43
view.setTag(viewHolder); //将viewHolder存储在view中
44
//如果Recycler中没有能重用的view,则创建新的(ListView中的一个子项)
45
}else{
46
view = convertView;
47
viewHolder = (ViewHolder)view.gettag(); //重新获取ViewHolder
48
}
49
/*
50
ImageView colorImage = (ImageView)view.findViewById(R.id.iv); //分别获取item内控件的实例
51
TextView colorText = (TextView)view.findViewById(R.id.tv);
52
colorImage.setImageResource(color.getImageId()); //分别调用set方法来设置控件内部显示的图片和文字
53
colorText.setText(color.getName());
54
*/
55
viewHolder.colorImage.setImageResource(color.getImageId());
56
viewHolder.colorName.setText(color.getNmae());
57
return view; //将设置完的布局返回
58
}
59
60
class ViewHolder{ //定义类,用来保存findViewById返回的实例
61
ImageView colorImage;
62
TextView colorName;
63
}
64
}
65
4、创建一个ArrayList对象,用作数据和item之间的桥梁
66
private List<Color> colorlist = new ArrayList<Color>();
67
5、由于得初始化item中的数据,可以将ArrayList的初始化封装在一个初始化函数中,在加载完布局文件之后调用初始化即可
68
private void initColor(){
69
Color black = new Color("black",R.drawable.black);
70
colorlist.add(black); //把创建好的对象添加到colorlist中
71
Color pink = new Color("pink",R.drawable.pink);
72
colorlist.add(pink);
73
Color blue = new Color("blue",R.drawable.blue);
74
colorlist.add(blue);
75
Color red = new Color("red",R.drawable.red);
76
colorlist.add(red);
77
}
78
6、创建自定义适配器的对象,然后为ListView设置监听器即可
79
ColorAdapter adapter = new ColorAdapter(MainActivity.this, R.layout.color_item,colorlist);
80
7、为ListView设置监听器
81
lv.setOnItemClickListener(new OnItemClickListener() {
82
@Override
83
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
84
Toast.makeText(MainActivity.this, colorList.get(position).getName(), Toast.LENGTH_SHORT).show();
85
}
86
});
获取更多学习资料+源码+笔记请加QQ3522655328(点击直接加QQ)