android之Listview的分組實現

來源:互聯網
上載者:User

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

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

       首先是main.xml布局:

<?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:

<?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

<?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>

   布局檔案我們已經實現了,下面看下在程式中我們是怎麼處理的吧!

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{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn list.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}        @Override        public boolean isEnabled(int position) {        // TODO Auto-generated method stub         if(groupkey.contains(getItem(position))){                 return false;             }             return super.isEnabled(position);        }@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView 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也沒多大區別,下面看看能不能實現呢

    運行一下:

  

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.