android之Listview的分组实现

 

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

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

       首先是main.xml布局:

 

[html]  view plain copy
  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:

 

 

[html]  view plain copy
  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

 

 

[html]  view plain copy
  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>  

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

 

 

[html]  view plain copy
  1. public class TestActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private List<String>  list=null;  
  4.     private List<String> groupkey=new ArrayList<String>();  
  5.      private List<String> aList = new ArrayList<String>();  
  6.       private List<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.         MyAdapter 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.         for(int i=0; i<5; i++){  
  26.             aList.add("A组"+i);  
  27.         }  
  28.         list.add("A组");  
  29.         list.addAll(aList);  
  30.           
  31.         for(int 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 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.             TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text);  
  75.             text.setText((CharSequence) getItem(position));  
  76.             return view;  
  77.         }  
  78.           
  79.     }  
  80. }  

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

 

    运行一下:

   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值