1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final
ArrayList<String> title =
new
ArrayList<String>();
for
(
int
i =
0
; i <
20
; i++)
{
title.add(
"标题"
+ i);
}
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(
new
BaseAdapter()
{
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
//执行数等于当前页面出现的行数,convertView指滚动消失的layout
{
Log.e(
"getView"
,
"调用getView"
+ i++ +
"convertView:"
+ convertView);
LayoutInflater inf = getLayoutInflater();
//通过LayoutInflater将布局id转换到对象
RelativeLayout relativeLayout = (RelativeLayout) inf.inflate(R.layout.relat,
null
);
TextView titleview = (TextView) relativeLayout.findViewById(R.id.textView1);
//通过布局找到里面的控件
titleview.setText(title.get(position));
//得到容器内的内容
return
relativeLayout;
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
Object getItem(
int
position)
{
return
null
;
}
@Override
//先执行getCount再执行getView,出现新行数时再次执行getCount
//listview清单里要设置高度match_parent
//每次点击屏幕一次都会先执行getCount
public
int
getCount()
{
Log.e(
"getCount"
,
"调用getCount"
+ j++);
return
title.size();
}
});
}
|
复用converview:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(
new
BaseAdapter()
{
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
//执行数等于当前页面出现的行数,convertView指滚动消失的layout
{
//复用converview
RelativeLayout relativeLayout =
null
;
if
(convertView ==
null
)
{
LayoutInflater inf = getLayoutInflater();
relativeLayout = (RelativeLayout) inf.inflate(R.layout.relat,
null
);
}
else
{
relativeLayout = (RelativeLayout) convertView;
}
TextView titleView = (TextView) relativeLayout.findViewById(R.id.textView1);
titleView.setText(title.get(position));
return
relativeLayout;
}
|
继续优化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
public
class
MainActivity
extends
Activity
{
int
imID =
0
;
class
Msg
//内容
{
String mTitle;
String mSubtitle;
String mTime;
int
mImagID;
public
Msg(String mTitle, String mSubtitle, String mTime,
int
mImagID)
{
super
();
this
.mTitle = mTitle;
this
.mSubtitle = mSubtitle;
this
.mTime = mTime;
this
.mImagID = mImagID;
}
}
class
ViewHolder
//控件
{
TextView viewTitle;
TextView viewSubtitle;
TextView viewTime;
ImageView viewImage;
}
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Date timeNow =
new
Date();
SimpleDateFormat timeFormat =
new
SimpleDateFormat(
"HH:mm:ss"
);
String time = timeFormat.format(timeNow);
final
ArrayList<Msg> msgList =
new
ArrayList<Msg>();
for
(
int
i =
0
; i <
30
; i++)
//选择图片
{
if
(i %
4
==
0
)
{
imID = R.drawable.ic_launcher;
}
if
(i %
4
==
1
)
{
imID = R.drawable.ic_1;
}
if
(i %
4
==
2
)
{
imID = R.drawable.ic_2;
}
if
(i %
4
==
3
)
{
imID = R.drawable.ic_3;
}
Msg msg =
new
Msg(
"标题"
+ i,
"内容"
+ i, time ,imID);
msgList.add(msg);
}
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(
new
BaseAdapter()
{
ViewHolder holder =
null
;
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
RelativeLayout relout =
null
;
if
(convertView ==
null
)
//创建首页时才find控件
{
LayoutInflater layoutInflater =getLayoutInflater();
relout = (RelativeLayout) layoutInflater.inflate(R.layout.relaout,
null
);
holder =
new
ViewHolder();
holder.viewTitle= (TextView) relout.findViewById(R.id.textView_title);
holder.viewSubtitle = (TextView) relout.findViewById(R.id.textView_subtitle);
holder.viewTime = (TextView) relout.findViewById(R.id.textView_time);
holder.viewImage = (ImageView) relout.findViewById(R.id.imageView1);
relout.setTag(holder);
}
else
//否则复用layout和view
{
relout = (RelativeLayout) convertView;
holder = (ViewHolder) relout.getTag();
//记得得到holder
}
Msg msg = msgList.get(position);
holder.viewTitle.setText(msg.mTitle);
holder.viewSubtitle.setText(msg.mSubtitle);
holder.viewTime.setText(msg.mTime);
holder.viewImage.setImageResource(msg.mImagID);
//设置图片的方法
return
relout;
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
Object getItem(
int
position)
{
return
null
;
}
@Override
public
int
getCount()
{
return
msgList.size();
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
点击按钮更新listview:Adapter使用notifyDataSetChanged()通知listview更新
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
void
btn1Click()
//增加一行信息
{
i++;
data.add(
"内容"
+ i );
arrAdapter.notifyDataSetChanged();
//Adapter通知listview做更新
}
private
void
btn2Click()
//删除第一行信息
{
if
(data.size() <
1
)
{
Toast.makeText(
this
,
"信息已删除完毕!"
, Toast.LENGTH_LONG).show();
}
else
{
data.remove(
0
);
arrAdapter.notifyDataSetChanged();
//Adapter通知listview做更新
}
}
private
void
btnsortClick()
//对列表进行排序
{
Collections.sort(data);
arrAdapter.notifyDataSetChanged();
}
|
注:
1.LayoutInflater inflater = getLayoutInflater();
layout = (RelativeLayout) inflater.inflate(R.layout.relayout, null);
layout要通过LayoutInflater拿到。
2.getView返回layout,getCount()返回int。
3.layout.getTag()要转换为holder = (ViewHolder)。
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1201270,如需转载请自行联系原作者