1. 程式人生 > >Android listview實現分組

Android listview實現分組

  對於Listview的分組我們再熟悉不過了,因為Android自帶的通訊錄中的聯絡人資訊就是使用的ListView分組,最近專案中用到了這個功能。所以趁著週末有時間,也更新下一篇這樣的部落格,希望對大家能夠有幫助。

       其實對於分組的ListView和我們平時用的ListView沒有多大差別,就是需要在介面卡中的getView方法中做下判斷。只要理解了這個,下面就好說了,下面我們看下實現程式碼。

       首先是main.xml佈局:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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.     <ListView
  8.         android:id="@+id/listView_list"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content">
  11.     </
    ListView>
  12. </LinearLayout>

      因為listview要載入兩種不同的item,所以要實現兩個item佈局,addexam_list_item.xml:
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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.   <ImageView
  8.        android:id="@+id/addexam_list_icon"
  9.        android:background="@drawable/ic_launcher"
  10.        android:layout_width="wrap_content"
  11.        android:layout_height="wrap_content"/>
  12.     <TextView
  13.        android:id="@+id/addexam_list_item_text"
  14.        android:layout_width="wrap_content"
  15.        android:layout_height="wrap_content"
  16.        android:layout_gravity="center"
  17.        android:layout_marginLeft="10dp"
  18.        android:text="測試資料"/>
  19. </LinearLayout>

     分組標籤對應的佈局addexam_list_item_tag.xml
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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.  <TextView
  10.        android:id="@+id/addexam_list_item_text"
  11.        android:layout_width="wrap_content"
  12.        android:layout_height="20dip"
  13.        android:textColor="#ffffff"
  14.        android:text="金融考試"
  15.        android:gravity="center_vertical"/>
  16. </LinearLayout>

   佈局檔案我們已經實現了,下面看下在程式中我們是怎麼處理的吧!
  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.         listview=(ListView) findViewById(R.id.listView_list);  
  13.         initData();  
  14.         MyAdapter adapter=new MyAdapter();  
  15.         listview.setAdapter(adapter);  
  16.     }  
  17.     public void initData(){  
  18.         list = new ArrayList<String>();  
  19.         groupkey.add("A組");  
  20.         groupkey.add("B組");  
  21.         for(int i=0; i<5; i++){  
  22.             aList.add("A組"+i);  
  23.         }  
  24.         list.add("A組");  
  25.         list.addAll(aList);  
  26.         for(int i=0; i<8; i++){  
  27.             bList.add("B組"+i);  
  28.         }  
  29.         list.add("B組");  
  30.         list.addAll(bList);  
  31.     }  
  32.     private class MyAdapter extends BaseAdapter{  
  33.         @Override  
  34.         public int getCount() {  
  35.             // TODO Auto-generated method stub  
  36.             return list.size();  
  37.         }  
  38.         @Override  
  39.         public Object getItem(int position) {  
  40.             // TODO Auto-generated method stub  
  41.             return list.get(position);  
  42.         }  
  43.         @Override  
  44.         public long getItemId(int position) {  
  45.             // TODO Auto-generated method stub  
  46.             return position;  
  47.         }  
  48.         @Override  
  49.         public boolean isEnabled(int position) {  
  50.             // TODO Auto-generated method stub  
  51.              if(groupkey.contains(getItem(position))){  
  52.                  return false;  
  53.              }  
  54.              return super.isEnabled(position);  
  55.         }  
  56.         @Override  
  57.         public View getView(int position, View convertView, ViewGroup parent) {  
  58.             // TODO Auto-generated method stub  
  59.             View view=convertView;  
  60.             if(groupkey.contains(getItem(position))){  
  61.                 view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);  
  62.             }else{  
  63.                 view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);  
  64.             }  
  65.             TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text);  
  66.             text.setText((CharSequence) getItem(position));  
  67.             return view;  
  68.         }  
  69.     }  
  70. }  

   程式碼好像挺簡單,更我們平時使用lsitview也沒多大區別,下面看看能不能實現呢

    執行一下:

  

轉自http://blog.csdn.net/wangkuifeng0118/article/details/7460142