android之Listview的分組實現

對於Listview的分組我們再熟悉不過瞭,因為Android自帶的通訊錄中的聯系人信息就是使用的ListView分組,最近項目中用到瞭這個功能。所以趁著周末有時間,也更新下一篇這樣的博客,希望對大傢能夠有幫助。

       其實對於分組的ListView和我們平時用的ListView沒有多大差別,就是需要在適配器中的getView方法中做下判斷。隻要理解瞭這個,下面就好說瞭,下面我們看下實現代碼。

       首先是main.xml佈局:

[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    android:orientation="vertical" > 
 
    <ListView 
        android:id="@+id/listView_list" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 
    </ListView> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
      因為listview要加載兩種不同的item,所以要實現兩個item佈局,addexam_list_item.xml:

[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"  
    android:padding="5dip"> 
     
  <ImageView 
       android:id="@+id/addexam_list_icon" 
       android:background="@drawable/ic_launcher" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
    <TextView 
       android:id="@+id/addexam_list_item_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginLeft="10dp" 
       android:text="測試數據"/> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip">
   
  <ImageView
       android:id="@+id/addexam_list_icon"
       android:background="@drawable/ic_launcher"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
    <TextView
       android:id="@+id/addexam_list_item_text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:layout_marginLeft="10dp"
       android:text="測試數據"/>
</LinearLayout>
     分組標簽對應的佈局addexam_list_item_tag.xml

[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#666666" 
    android:paddingLeft="10dp" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 
     
 <TextView 
       android:id="@+id/addexam_list_item_text" 
       android:layout_width="wrap_content" 
       android:layout_height="20dip" 
       android:textColor="#ffffff" 
       android:text="金融考試" 
       android:gravity="center_vertical"/> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#666666"
    android:paddingLeft="10dp"
    android:gravity="center_vertical"
    android:orientation="vertical" >
   
 <TextView
       android:id="@+id/addexam_list_item_text"
       android:layout_width="wrap_content"
       android:layout_height="20dip"
       android:textColor="#ffffff"
       android:text="金融考試"
       android:gravity="center_vertical"/>
</LinearLayout>
   佈局文件我們已經實現瞭,下面看下在程序中我們是怎麼處理的吧!

[html] public class TestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private List<String>  list=null; 
    private List<String> groupkey=new ArrayList<String>(); 
     private List<String> aList = new ArrayList<String>(); 
      private List<String> bList = new ArrayList<String>(); 
    private ListView listview; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        listview=(ListView) findViewById(R.id.listView_list); 
        initData(); 
        MyAdapter adapter=new MyAdapter(); 
        listview.setAdapter(adapter); 
         
    } 
    public void initData(){ 
        list = new ArrayList<String>(); 
         
        groupkey.add("A組"); 
        groupkey.add("B組"); 
         
        for(int i=0; i<5; i++){ 
            aList.add("A組"+i); 
        } 
        list.add("A組"); 
        list.addAll(aList); 
         
        for(int i=0; i<8; i++){ 
            bList.add("B組"+i); 
        } 
        list.add("B組"); 
        list.addAll(bList); 
    } 
     
    private class MyAdapter extends BaseAdapter{ 
 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return list.size(); 
        } 
 
        @Override 
        public Object getItem(int position) { 
            // TODO Auto-generated method stub 
            return list.get(position); 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub 
            return position; 
        } 
        @Override 
        public boolean isEnabled(int position) { 
            // TODO Auto-generated method stub 
             if(groupkey.contains(getItem(position))){ 
                 return false; 
             } 
             return super.isEnabled(position); 
        } 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            // TODO Auto-generated method stub 
            View view=convertView; 
            if(groupkey.contains(getItem(position))){ 
                view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null); 
            }else{ 
                view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null); 
            } 
            TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text); 
            text.setText((CharSequence) getItem(position)); 
            return view; 
        } 
         
    } 

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
 private List<String>  list=null;
 private List<String> groupkey=new ArrayList<String>();
  private List<String> aList = new ArrayList<String>();
   private List<String> bList = new ArrayList<String>();
 private ListView listview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        listview=(ListView) findViewById(R.id.listView_list);
        initData();
        MyAdapter adapter=new MyAdapter();
        listview.setAdapter(adapter);
       
    }
    public void initData(){
        list = new ArrayList<String>();
       
        groupkey.add("A組");
        groupkey.add("B組");
       
        for(int i=0; i<5; i++){
         aList.add("A組"+i);
        }
        list.add("A組");
        list.addAll(aList);
       
        for(int i=0; i<8; i++){
            bList.add("B組"+i);
        }
        list.add("B組");
        list.addAll(bList);
    }
   
    private class MyAdapter extends BaseAdapter{

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return list.size();
  }

  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return list.get(position);
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;
  }
        @Override
        public boolean isEnabled(int position) {
         // TODO Auto-generated method stub
          if(groupkey.contains(getItem(position))){
                 return false;
             }
             return super.isEnabled(position);
        }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   View view=convertView;
   if(groupkey.contains(getItem(position))){
    view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item_tag, null);
   }else{
    view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.addexam_list_item, null);
   }
   TextView text=(TextView) view.findViewById(R.id.addexam_list_item_text);
   text.setText((CharSequence) getItem(position));
   return view;
  }
     
    }
}
   代碼好像挺簡單,更我們平時使用lsitview也沒多大區別,下面看看能不能實現呢

  運行一下:

摘自 wangkuifeng0118的專欄
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *