android之Listview的分组实现
  RhNPwA0V2ypD 2023年11月02日 38 0


 



对于Listview的分组我们再熟悉不过了,因为Android 自带的通讯录中的联系人 信息就是使用的ListView分组,最近项目中用到了这个功能。所以趁着周末有时间,也更新下一篇这样的博客,希望对大家能够有帮助。



       其实对于分组的ListView和我们平时用的ListView没有多大差别,就是需要在适配器中的getView方法中做下判断。只要理解了这个,下面就好说了,下面我们看下实现代码。

       首先是main.xml布局:

 




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="fill_parent"  
5. android:background="#ffffff"  
6. android:orientation="vertical" >  
7.   
8. <ListView  
9. android:id="@+id/listView_list"  
10. android:layout_width="match_parent"  
11. android:layout_height="wrap_content" >  
12. </ListView>  
13.   
14. </LinearLayout>


      因为listview要加载两种不同的item,所以要实现两个item布局,addexam_list_item.xml:


 

 




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:orientation="horizontal"   
6. android:padding="5dip">  
7.       
8. <ImageView  
9. android:id="@+id/addexam_list_icon"  
10. android:background="@drawable/ic_launcher"  
11. android:layout_width="wrap_content"  
12. android:layout_height="wrap_content"/>  
13. <TextView  
14. android:id="@+id/addexam_list_item_text"  
15. android:layout_width="wrap_content"  
16. android:layout_height="wrap_content"  
17. android:layout_gravity="center"  
18. android:layout_marginLeft="10dp"  
19. android:text="测试数据"/>  
20. </LinearLayout>

     分组标签对应的布局addexam_list_item_tag.xml


 

 



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="#666666"  
6. android:paddingLeft="10dp"  
7. android:gravity="center_vertical"  
8. android:orientation="vertical" >  
9.       
10. <TextView  
11. android:id="@+id/addexam_list_item_text"  
12. android:layout_width="wrap_content"  
13. android:layout_height="20dip"  
14. android:textColor="#ffffff"  
15. android:text="金融考试"  
16. android:gravity="center_vertical"/>  
17. </LinearLayout>


   布局文件我们已经实现了,下面看下在程序中我们是怎么处理的吧!


 

 



1. public class TestActivity extends Activity {  
2.     /** Called when the activity is first created. */  
3. <String>  list=null;  
4. <String> groupkey=new ArrayList<String>();  
5. <String> aList = new ArrayList<String>();  
6. <String> bList = new ArrayList<String>();  
7.     private ListView listview;  
8.     @Override  
9.     public void onCreate(Bundle savedInstanceState) {  
10.         super.onCreate(savedInstanceState);  
11.         setContentView(R.layout.main);  
12.           
13. listview=(ListView) findViewById(R.id.listView_list);  
14.         initData();  
15. adapter=new MyAdapter();  
16.         listview.setAdapter(adapter);  
17.           
18.     }  
19.     public void initData(){  
20. list = new ArrayList<String>();  
21.           
22.         groupkey.add("A组");  
23.         groupkey.add("B组");  
24.           
25. i=0; i<5; i++){  
26.             aList.add("A组"+i);  
27.         }  
28.         list.add("A组");  
29.         list.addAll(aList);  
30.           
31. i=0; i<8; i++){  
32.             bList.add("B组"+i);  
33.         }  
34.         list.add("B组");  
35.         list.addAll(bList);  
36.     }  
37.       
38.     private class MyAdapter extends BaseAdapter{  
39.   
40.         @Override  
41.         public int getCount() {  
42.             // TODO Auto-generated method stub  
43.             return list.size();  
44.         }  
45.   
46.         @Override  
47.         public Object getItem(int position) {  
48.             // TODO Auto-generated method stub  
49.             return list.get(position);  
50.         }  
51.   
52.         @Override  
53.         public long getItemId(int position) {  
54.             // TODO Auto-generated method stub  
55.             return position;  
56.         }  
57.         @Override  
58.         public boolean isEnabled(int position) {  
59.             // TODO Auto-generated method stub  
60.              if(groupkey.contains(getItem(position))){  
61.                  return false;  
62.              }  
63.              return super.isEnabled(position);  
64.         }  
65.         @Override  
66.         public View getView(int position, View convertView, ViewGroup parent) {  
67.             // TODO Auto-generated method stub  
68. view=convertView;  
69.             if(groupkey.contains(getItem(position))){  
70. view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);  
71.             }else{  
72. view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);  
73.             }  
74. text=(TextView) view.findViewById(R.id.addexam_list_item_text);  
75.             text.setText((CharSequence) getItem(position));  
76.             return view;  
77.         }  
78.           
79.     }  
80. }


   代码好像挺简单,更我们平时使用lsitview也没多大区别,下面看看能不能实现呢


 

    运行一下:

   

android之Listview的分组实现_ide

 


       其实对于分组的ListView和我们平时用的ListView没有多大差别,就是需要在适配器中的getView方法中做下判断。只要理解了这个,下面就好说了,下面我们看下实现代码。

       首先是main.xml布局:

 



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="fill_parent"  
5. android:background="#ffffff"  
6. android:orientation="vertical" >  
7.   
8. <ListView  
9. android:id="@+id/listView_list"  
10. android:layout_width="match_parent"  
11. android:layout_height="wrap_content" >  
12. </ListView>  
13.   
14. </LinearLayout>



      因为listview要加载两种不同的item,所以要实现两个item布局,addexam_list_item.xml:


 

 




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:orientation="horizontal"   
6. android:padding="5dip">  
7.       
8. <ImageView  
9. android:id="@+id/addexam_list_icon"  
10. android:background="@drawable/ic_launcher"  
11. android:layout_width="wrap_content"  
12. android:layout_height="wrap_content"/>  
13. <TextView  
14. android:id="@+id/addexam_list_item_text"  
15. android:layout_width="wrap_content"  
16. android:layout_height="wrap_content"  
17. android:layout_gravity="center"  
18. android:layout_marginLeft="10dp"  
19. android:text="测试数据"/>  
20. </LinearLayout>


     分组标签对应的布局addexam_list_item_tag.xml


 

 



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="#666666"  
6. android:paddingLeft="10dp"  
7. android:gravity="center_vertical"  
8. android:orientation="vertical" >  
9.       
10. <TextView  
11. android:id="@+id/addexam_list_item_text"  
12. android:layout_width="wrap_content"  
13. android:layout_height="20dip"  
14. android:textColor="#ffffff"  
15. android:text="金融考试"  
16. android:gravity="center_vertical"/>  
17. </LinearLayout>

   布局文件我们已经实现了,下面看下在程序中我们是怎么处理的吧!


 

 




1. public class TestActivity extends Activity {  
2.     /** Called when the activity is first created. */  
3. <String>  list=null;  
4. <String> groupkey=new ArrayList<String>();  
5. <String> aList = new ArrayList<String>();  
6. <String> bList = new ArrayList<String>();  
7.     private ListView listview;  
8.     @Override  
9.     public void onCreate(Bundle savedInstanceState) {  
10.         super.onCreate(savedInstanceState);  
11.         setContentView(R.layout.main);  
12.           
13. listview=(ListView) findViewById(R.id.listView_list);  
14.         initData();  
15. adapter=new MyAdapter();  
16.         listview.setAdapter(adapter);  
17.           
18.     }  
19.     public void initData(){  
20. list = new ArrayList<String>();  
21.           
22.         groupkey.add("A组");  
23.         groupkey.add("B组");  
24.           
25. i=0; i<5; i++){  
26.             aList.add("A组"+i);  
27.         }  
28.         list.add("A组");  
29.         list.addAll(aList);  
30.           
31. i=0; i<8; i++){  
32.             bList.add("B组"+i);  
33.         }  
34.         list.add("B组");  
35.         list.addAll(bList);  
36.     }  
37.       
38.     private class MyAdapter extends BaseAdapter{  
39.   
40.         @Override  
41.         public int getCount() {  
42.             // TODO Auto-generated method stub  
43.             return list.size();  
44.         }  
45.   
46.         @Override  
47.         public Object getItem(int position) {  
48.             // TODO Auto-generated method stub  
49.             return list.get(position);  
50.         }  
51.   
52.         @Override  
53.         public long getItemId(int position) {  
54.             // TODO Auto-generated method stub  
55.             return position;  
56.         }  
57.         @Override  
58.         public boolean isEnabled(int position) {  
59.             // TODO Auto-generated method stub  
60.              if(groupkey.contains(getItem(position))){  
61.                  return false;  
62.              }  
63.              return super.isEnabled(position);  
64.         }  
65.         @Override  
66.         public View getView(int position, View convertView, ViewGroup parent) {  
67.             // TODO Auto-generated method stub  
68. view=convertView;  
69.             if(groupkey.contains(getItem(position))){  
70. view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);  
71.             }else{  
72. view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);  
73.             }  
74. text=(TextView) view.findViewById(R.id.addexam_list_item_text);  
75.             text.setText((CharSequence) getItem(position));  
76.             return view;  
77.         }  
78.           
79.     }  
80. }


   代码好像挺简单,更我们平时使用lsitview也没多大区别,下面看看能不能实现呢


 

    运行一下:

   

android之Listview的分组实现_ide

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  F36IaJwrKLcw   2023年12月23日   38   0   0 idesparkidesparkDataData
  FoZN5OJ14wRT   2023年11月28日   28   0   0 Luaideideluasedsed
  dMIEwfNiKi33   2023年12月05日   32   0   0 hivehivexmlxml
RhNPwA0V2ypD
最新推荐 更多

2024-05-31