Bài 14: Custom Layout cho ListView trong Android(P1)

Leave a Comment
Tôi có làm một ví dụ về quản lý nhân viên với giao diện bên dưới đây:
14_custom_0- Bạn quan sát là phần danh sách nhân viên bên dưới là Custom Layout.
- Mỗi dòng trong ListView sẽ có 3 đối tượngImageViewTextView và Checkbox.
- Khi nhập nhân viên nếu người sử dụng chọn Nữ thì sẽ hiển thị hình là con gái, nếu chọn Nam thì hiển thị hình là con trai (bạn nhìn danh sách hình nhỏ nhỏ 16×16 ở ListView).
- Mã và tên của nhân viên sẽ được hiển thị vào TextView
- Checkbox cho phép người sử dụng checked (nhằm đánh dấu những nhân viên muốn xóa, ở đây cho phép xóa nhiều nhân viên)
- Bạn để ý Tôi có thêm 1 ImageButton có hình màu Chéo đỏ, nó dùng để xóa tất cả các nhân viên được Checked trong ListView, sau khi xóa thành công thì phải cập nhật lại ListView.
- Để làm được điều trên thì ta sẽ kế thừa từ ArrayAdapter và override phương thức getView, cụ thể:
- Bạn xem Cấu trúc chương trình quản lý nhân viên:
14_custom_1
- Tôi tạo thêm thư mục drawable và kéo thả 3 icon mà Tôi sử dụng vào (bạn cũng tạo thư mục tên y xì vậy). Nhớ là tên hình phải viết liền và chữ thường đầu tiên.
- Trong thư mục layout: Tôi tạo thêm my_item_layout.xml dùng để Custom lại ListView, dưới đây là cấu trúc XML của nó:
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
<ImageView
 android:id="@+id/imgitem"
 android:layout_width="22dip"
 android:layout_height="22dip"
 android:paddingLeft="2dp"
 android:paddingRight="2dp"
 android:paddingTop="2dp"
 android:layout_marginTop="4dp"
 android:contentDescription="here"
 android:src="@drawable/ic_launcher" />
<TextView
 android:id="@+id/txtitem"
 android:layout_height="wrap_content"
 android:layout_width="0dip"
 android:layout_weight="2"
 android:layout_marginTop="4dp"
 android:paddingLeft="2dp"
 android:paddingRight="2dp"
 android:paddingTop="2dp"
 android:textSize="15sp" />
<CheckBox
 android:id="@+id/chkitem"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>
- Ta sẽ dựa vào các id trong này để xử lý trong hàm getView của class mà ta kế thừa từ ArrayAdapter (các id trên là imgitem đại diện cho hình là Nữ hay Nam, txtitem dùng để hiển thị mã và tên nhân viên, chkitem dùng để xử lý Checked)
- Bạn xem activity_main.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
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >
<TextView
 android:id="@+id/textView2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#008000"
 android:gravity="center"
 android:text="Quản lý nhân viên"
 android:textColor="#FFFFFF"
 android:textSize="20sp" />
<TableLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:stretchColumns="*"
 >
<TableRow
 android:id="@+id/tableRow1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Mã NV:" />
<EditText
 android:id="@+id/editMa"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:ems="10" >
<requestFocus />
 </EditText>
</TableRow>
<TableRow
 android:id="@+id/tableRow2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Tên NV:" />
<EditText
 android:id="@+id/editTen"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:ems="10" />
</TableRow>
<TableRow
 android:id="@+id/tableRow3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Giới tính:" />
<RadioGroup
 android:id="@+id/radioGroup1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >
<RadioButton
 android:id="@+id/radNu"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:checked="true"
 android:text="Nữ" />
<RadioButton
 android:id="@+id/radNam"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Nam" />
 </RadioGroup>
</TableRow>
<TableRow
 android:id="@+id/tableRow4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<Button
 android:id="@+id/btnNhap"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_column="1"
 android:text="Nhập NV" />
</TableRow>
 </TableLayout>
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="9"
 android:background="#008000"
 android:layout_marginTop="2dp"
 android:text="Danh sách nhân viên:"
 android:textColor="#FFFFFF"
 android:textSize="20sp" />
<ImageButton
 android:id="@+id/btndelete"
 android:layout_width="30dip"
 android:layout_height="30dip"
 android:src="@drawable/deleteicon" />
</LinearLayout>
<ListView
 android:id="@+id/lvnhanvien"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
 </ListView>
</LinearLayout>
Xem tiếp p2

0 nhận xét:

Đăng nhận xét