Android中的ListView实现图片文字和按钮
来源:互联网 发布:环信 java 服务端 编辑:程序博客网 时间:2024/05/22 06:50
实现效果图:
布局文件:
vlist2.xml
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
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"horizontal"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
ImageView
android:id
=
"@+id/img"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_margin
=
"5px"
/>
<
LinearLayout
android:orientation
=
"vertical"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
<
TextView
android:id
=
"@+id/title"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"#FFFFFFFF"
android:textSize
=
"22px"
/>
<
TextView
android:id
=
"@+id/info"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"#FFFFFFFF"
android:textSize
=
"13px"
/>
</
LinearLayout
>
<
Button
android:id
=
"@+id/view_btn"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/s_view_btn"
android:layout_gravity
=
"bottom|right"
/>
</
LinearLayout
>
程序代码:
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @author allin
*
*/
public
class
MyListView4
extends
ListActivity {
private
List<Map<String, Object>> mData;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter =
new
MyAdapter(
this
);
setListAdapter(adapter);
}
private
List<Map<String, Object>> getData() {
List<Map<String, Object>> list =
new
ArrayList<Map<String, Object>>();
Map<String, Object> map =
new
HashMap<String, Object>();
map.put(
"title"
,
"G1"
);
map.put(
"info"
,
"google 1"
);
map.put(
"img"
, R.drawable.i1);
list.add(map);
map =
new
HashMap<String, Object>();
map.put(
"title"
,
"G2"
);
map.put(
"info"
,
"google 2"
);
map.put(
"img"
, R.drawable.i2);
list.add(map);
map =
new
HashMap<String, Object>();
map.put(
"title"
,
"G3"
);
map.put(
"info"
,
"google 3"
);
map.put(
"img"
, R.drawable.i3);
list.add(map);
return
list;
}
// ListView 中某项被选中后的逻辑
@Override
protected
void
onListItemClick(ListView l, View v,
int
position,
long
id) {
Log.v(
"MyListView4-click"
, (String)mData.get(position).get(
"title"
));
}
/**
* listview中点击按键弹出对话框
*/
public
void
showInfo(){
new
AlertDialog.Builder(
this
)
.setTitle(
"我的listview"
)
.setMessage(
"介绍..."
)
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
})
.show();
}
public
final
class
ViewHolder{
public
ImageView img;
public
TextView title;
public
TextView info;
public
Button viewBtn;
}
public
class
MyAdapter
extends
BaseAdapter{
private
LayoutInflater mInflater;
public
MyAdapter(Context context){
this
.mInflater = LayoutInflater.from(context);
}
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
mData.size();
}
@Override
public
Object getItem(
int
arg0) {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
long
getItemId(
int
arg0) {
// TODO Auto-generated method stub
return
0
;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
ViewHolder holder =
null
;
if
(convertView ==
null
) {
holder=
new
ViewHolder();
convertView = mInflater.inflate(R.layout.vlist2,
null
);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(position).get(
"img"
));
holder.title.setText((String)mData.get(position).get(
"title"
));
holder.info.setText((String)mData.get(position).get(
"info"
));
holder.viewBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
showInfo();
}
});
return
convertView;
}
}
}
- Android中的ListView实现图片文字和按钮
- Android ListView常用用法 (实现文字图片和按钮)
- android实现自定义图片+文字按钮
- Android图片文字按钮
- TabHosts导航按钮实现自定义图片和文字
- Android在Button按钮上同时显示文字和图片
- Android中ListView的item中添加图片和文字
- Android 点击按钮实现来回切换背景图和文字
- listView里面嵌套自定义的图片,文字,按钮
- android listview 中点击按钮实现跳转和删除
- Android ListView ArrayAdapter同时实现列表和按钮点击事件
- 按钮图片和文字上下居中对齐
- 按钮上同时有文字和图片
- 自定义按钮图片和文字的位置
- 使用poi替换word中的文字和图片实现打印
- listview使用BaseAdapter显示图片和文字
- listview使用BaseAdapter显示图片和文字
- listview使用SimpleAdapter显示图片和文字
- JS
- 【leetcode】Surrounded Regions
- 由C/C++中的结构体包含数组引申到C/C++堆栈
- Intel-最快线程间数据交换算法,有效避免锁竞争 -- TwoQueues
- Vim高级进阶之ex命令集
- Android中的ListView实现图片文字和按钮
- XDU 1196
- img 灰度阈值变化
- Android开发之DOS命令行的使用
- sed的一些用法
- AndroidManifest.xml文件versionCode和versionName详解
- 构造方法和对象的创建简单例题
- linux之上C内嵌汇编并转换虚拟/物理地址
- 盛大陈天桥:最好的商业模式要深谙“人性”