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
|
public
class
MainActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String, String>> data =
new
ArrayList<HashMap<String,String>>();
for
(
int
i =
0
; i <
30
; i++)
{
HashMap<String, String> object =
new
HashMap<String, String>();
object.put(
"title"
,
"title"
+ i );
object.put(
"subtitle"
,
"subtitle"
+ i );
object.put(
"time"
,
"time"
+ i );
data.add(object);
}
String[] from =
new
String[]{
"title"
,
"subtitle"
,
"time"
};
//作为hashmap的key使用
int
[] to =
new
int
[]{R.id.textView_title,R.id.textView_subtitle,R.id.textView_time};
SimpleAdapter simpleAdapter =
new
SimpleAdapter(
this
, data, R.layout.relaout, from, to);
listView.setAdapter(simpleAdapter);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
注:SimpleAdapter支持的控件有限。
==================================================================
二、设置不同行布局
代码如下:
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
|
class
WeiboListAdapter
extends
BaseAdapter
{
private
final
static
int
VIEW_TYPE1 =
0
;
private
final
static
int
VIEW_TYPE2 =
1
;
private
final
static
int
VIEW_TYPE_COUNT = VIEW_TYPE2 +
1
;
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
CommentViewHolder commentHolder =
null
;
ViewHolder holder =
null
;
int
type = getItemViewType(position);
if
(convertView ==
null
)
{
if
(type == VIEW_TYPE1)
{
// 微博内容
convertView = getLayoutInflater().inflate(R.layout.weibo_item,
null
);
holder =
new
ViewHolder();
holder.headerImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_headerImg);
holder.userName = (TextView) convertView.findViewById(R.id.weibo_item_tv_userName);
holder.commentNum = (TextView) convertView.findViewById(R.id.weibo_item_tv_commentNum);
holder.forwardTimes = (TextView) convertView.findViewById(R.id.weibo_item_tv_forwardTimes);
holder.content = (TextView) convertView.findViewById(R.id.weibo_item_tv_content);
holder.contentImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_contentImg);
holder.forwardContent = (TextView) convertView.findViewById(R.id.weibo_item_tv_forwardContent);
holder.forwardContentImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_forwardContentImg);
holder.from = (TextView) convertView.findViewById(R.id.weibo_item_tv_from);
holder.time = (TextView) convertView.findViewById(R.id.weibo_item_tv_time);
holder.retweetedLayout = convertView.findViewById(R.id.weibo_item_retweetedLayout);
convertView.setTag(holder);
}
else
{
// 评论列表
commentHolder =
new
CommentViewHolder();
convertView = getLayoutInflater().inflate(R.layout.weibocomment_item,
null
);
commentHolder.headerImg = (ImageView) convertView.findViewById(R.id.weibocomment_item_iv_headerImg);
commentHolder.userName = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_userName);
commentHolder.time = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_time);
commentHolder.content = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_content);
convertView.setTag(commentHolder);
}
}
else
{
if
(type == VIEW_TYPE1)
{
holder = (ViewHolder) convertView.getTag();
}
else
{
commentHolder = (CommentViewHolder) convertView.getTag();
}
}
WeiboJson weiboJson = mDataList.get(position);
if
(type == VIEW_TYPE1)
{
updataWeiboData(holder, weiboJson);
}
else
{
updateCommentData(commentHolder, weiboJson);
}
return
convertView;
}
@Override
public
int
getCount()
{
return
mDataList.size();
}
@Override
public
int
getItemViewType(
int
position)
{
if
(position ==
0
)
{
return
VIEW_TYPE1;
}
else
{
return
VIEW_TYPE2;
}
}
@Override
public
int
getViewTypeCount()
{
return
VIEW_TYPE_COUNT;
}
@Override
public
Object getItem(
int
position)
{
return
mDataList.get(position);
}
@Override
public
long
getItemId(
int
position)
{
return
position;
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1202086,如需转载请自行联系原作者