Because it is an article reposted on the internet, I will not talk nonsense here. First, let's take a look at the final:
The main code for implementing the ListView layout is as follows:
1. Main Program Interface SeparateListView. java
[Java]
1. package whu. iss. wuxianglong;
2.
3. import java. util. ArrayList;
4. import java. util. List;
5.
6. import android. app. Activity;
7. import android. content. Context;
8. import android. OS. Bundle;
9. import android. view. LayoutInflater;
10. import android. view. View;
11. import android. view. ViewGroup;
12. import android. widget. ArrayAdapter;
13. import android. widget. ListView;
14. import android. widget. TextView;
15.
16. public class SeparateListView extends Activity {
17. ListView listView;
18. MyAdapter myAdapter;
19. public List <String> listTag = new ArrayList <String> ();
20.
21./** Called when the activity is first created .*/
22. @ Override
23. public void onCreate (Bundle savedInstanceState ){
24. super. onCreate (savedInstanceState );
25. setContentView (R. layout. main );
26.
27. listView = (ListView) findViewById (R. id. list );
28. myAdapter = new MyAdapter (this,
29. android. R. layout. simple_expandable_list_item_1, getData ());
30. listView. setAdapter (myAdapter );
31 .}
32. private List <String> getData (){
33. List <String> data = new ArrayList <String> ();
34. int I = 0;
35.
36. data. add ("");
37. listTag. add ("");
38. data. add ("aa trial data" + (I ++ ));
39. data. add ("a trial data" + (I ++ ));
40. data. add ("aa trial data" + (I ++ ));
41. listTag. add ("B ");
42. data. add ("B ");
43. data. add ("bb trial data" + (I ++ ));
44. data. add ("B trial data" + (I ++ ));
45. data. add ("B trial data" + (I ++ ));
46. data. add ("B trial data" + (I ++ ));
47. listTag. add ("C ");
48. data. add ("C ");
49. data. add ("c Test data" + (I ++ ));
50. data. add ("c Test data" + (I ++ ));
51. listTag. add ("D ");
52. data. add ("D ");
53. data. add ("d test data" + (I ++ ));
54. data. add ("d test data" + (I ++ ));
55. data. add ("d test data" + (I ++ ));
56. listTag. add ("E ");
57. data. add ("E ");
58. data. add ("e test data" + (I ++ ));
59. data. add ("e test data" + (I ++ ));
60. data. add ("e test data" + (I ++ ));
61. listTag. add ("F ");
62. data. add ("F ");
63. data. add ("f test data" + (I ++ ));
64. return data;
65 .}
66.
67.
68. class MyAdapter extends ArrayAdapter <String> {
69.
70. public MyAdapter (Context context, int textViewResourceId,
71. List <String> objects ){
72. super (context, textViewResourceId, objects );
73.
74 .}
75.
76. @ Override
77. public boolean areAllItemsEnabled (){
78. return false;
79 .}
80.
81. @ Override
82. public boolean isEnabled (int position ){
83. // This option is optional if it starts-
84. return! ListTag. contains (getItem (position ));
85 .}
86.
87. @ Override
88. public View getView (int position, View convertView, ViewGroup parent ){
89. View view = convertView;
90. // layout templates that cannot be loaded Based on tag types
91. if (listTag. contains (getItem (position ))){
92. // if it is a label item
93. view = LayoutInflater. from (getContext (). inflate (R. layout. group_list_item_tag, null );
94.} else {
95. // otherwise it is a data item
96. view = LayoutInflater. from (getContext (). inflate (R. layout. group_list_item, null );
97 .}
98. // display name
99. TextView textView = (TextView) view. findViewById (R. id. group_list_item_text );
100. textView. setText (getItem (position ));
101. // return the overwritten view
102. return view;
103 .}
104.
105 .}
106 .}
2. main. xml
[Html]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: orientation = "vertical"
4. android: layout_width = "fill_parent"
5. android: layout_height = "fill_parent"
6.>
7. <ListView
8. android: id = "@ + id/list"
9. android: layout_width = "fill_parent"
10. android: layout_height = "wrap_content">
11. </ListView>
12. </LinearLayout>
3. The layout file group_list_item.xml of some data styles in ListView
[Html]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: orientation = "horizontal"
4. android: layout_width = "fill_parent"
5. android: layout_height = "wrap_content"
6. android: padding = "5dip">
7. <ImageView
8. android: src = "@ drawable/icon"
9. android: layout_width = "50px"
10. android: layout_height = "50px">
11. </ImageView>
12. <TextView
13. android: id = "@ + id/group_list_item_text"
14. android: layout_width = "wrap_content"
15. android: layout_height = "fill_parent"
16. android: paddingLeft = "5dip"
17. android: gravity = "center_vertical">
18. </TextView>
19. </LinearLayout>
4. The style layout file group_list_item_tag.xml of the grouping flag row in ListView
[Html]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: layout_width = "fill_parent"
4. android: layout_height = "wrap_content"
5. android: background = "#555555"
6. android: paddingLeft = "10dip">
7. <TextView
8. android: id = "@ + id/group_list_item_text"
9. android: layout_width = "wrap_content"
10. android: layout_height = "20dip"
11. android: textColor = "# ffffff"
12. android: gravity = "center_vertical">
13. </TextView>
14. </LinearLayout>
Excerpt from advancing with the times