Android在ExpandableListView控制的基本使用

來源:互聯網
上載者:User

標籤:

             在本文中,Demo為了展示Android在ExpandableListView用途管制。如該組/兒子ListView綁定資料來源。

直接上代碼例如以下:

程式結構圖:

layout檔案夾下的 main.xml 檔案原始碼例如以下:

<?

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 我們會自訂listview的顯示方式(在另外一個布局檔案中邊)不用預設的方式 假設自訂listview的顯示方式這裡這個android:id="@id/android:list" 必須這樣寫 --> <!-- android:drawSelectOnTop="false"此屬性用來設定listview上的背景顏色會不會 擋住(覆蓋)內容 , 假設這是為false就表示不會覆蓋掉 --> <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false"/> </LinearLayout>


包 com.andyidea.demo中ContactsActivity.java原始碼例如以下:

package com.andyidea.demo;import java.util.ArrayList;import java.util.List;import android.app.ExpandableListActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class ContactsActivity extends ExpandableListActivity {List<String> group;           //組列表List<List<String>> child;     //子列表ContactsInfoAdapter adapter;  //資料配接器    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);  //設定為無標題        setContentView(R.layout.main);        getExpandableListView().setBackgroundResource(R.drawable.default_bg);                initializeData();        getExpandableListView().setAdapter(new ContactsInfoAdapter());        getExpandableListView().setCacheColorHint(0);  //設定拖動列表的時候防止出現黑色背景    }        /**     * 初始化組、子列表資料     */    private void initializeData(){    group = new ArrayList<String>();    child = new ArrayList<List<String>>();        addInfo("Andy",new String[]{"male","138123***","GuangZhou"});    addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});    addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});    addInfo("Tom",new String[]{"female","138123***","ShangHai"});    addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});        }        /**     * 類比給組、子列表加入資料     * @param g-group     * @param c-child     */    private void addInfo(String g,String[] c){    group.add(g);    List<String> childitem = new ArrayList<String>();    for(int i=0;i<c.length;i++){    childitem.add(c[i]);    }    child.add(childitem);    }        class ContactsInfoAdapter extends BaseExpandableListAdapter{        //-----------------Child----------------//    @Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}        @Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}        @Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}        @Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {    String string = child.get(groupPosition).get(childPosition); return getGenericView(string);}        //----------------Group----------------//    @Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic int getGroupCount() {return group.size();}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {String string = group.get(groupPosition);  return getGenericView(string);}//建立組/子視圖          public TextView getGenericView(String s) {              // Layout parameters for the ExpandableListView              AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                      ViewGroup.LayoutParams.FILL_PARENT, 40);              TextView text = new TextView(ContactsActivity.this);              text.setLayoutParams(lp);              // Center the text vertically              text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);              // Set the text starting position              text.setPadding(36, 0, 0, 0);                            text.setText(s);              return text;          }  @Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}        }}


最後,程式執行後例如以下:

       

Android在ExpandableListView控制的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.