Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ExpandableListView 分組列表控件完整實作教學

編輯:關於 Android 程式設計

ExpandableListView 是 Android 平台上一個非常實用的可展開列表控件,它能夠以「分組+子項」的兩層結構來展示資料,常見於分類目錄、設定選單等場景。本文將透過一個完整的實作範例,帶你從頭到尾走一遍它的用法。

第一步:在版面配置檔中宣告控件

首先在 layout 的 XML 檔案裡加入一個 ExpandableListView 節點,並為它指定 id,方便稍後在程式碼中抓取。

復制代碼 代碼如下:<LinearLayout  
    android:id="@+id/linearLayout" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    androidrientation="vertical" 
    > 
     
    <ExpandableListView 
    android:id="@+id/expandableListView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
        /> 
</LinearLayout>

第二步:準備兩組資料集合

接著定義兩個 List,一個用來存放各分組(Group)的標題文字,另一個則是二維的 List,用來存放每個分組底下對應的子項(Child)內容。

復制代碼 代碼如下:private List<String> groupArray; 
private List<List<String>> childArray;

第三步:初始化並填入測試資料

完成宣告後,我們對這兩個集合進行初始化,並塞入一些示範資料。這裡示範了兩個分組,每個分組都共用同一份子項清單。

復制代碼 代碼如下:groupArray = new ArrayList<String>(); 
childArray = new ArrayList<List<String>>(); 
 
groupArray.add("第一行"); 
groupArray.add("第二行"); 
 
List<String> tempArray = new ArrayList<String>(); 
tempArray.add("第一條"); 
tempArray.add("第二條"); 
tempArray.add("第三條"); 
 
for(int index = 0; index <groupArray.size(); ++index) 
{ 
    childArray.add(tempArray); 
}

第四步:自訂 Adapter 配接器

ExpandableListView 需要搭配一個繼承自 BaseExpandableListAdapter 的配接器才能運作。以下這個自訂的 ExpandableAdapter 負責決定每個分組與子項的外觀與資料來源。

復制代碼 代碼如下://ExpandableListView的Adapter 
public class ExpandableAdapter extends BaseExpandableListAdapter 
{ 
    Activity activity; 
     
    public ExpandableAdapter(Activity a) 
    { 
        activity = a; 
    } 
    public Object getChild(int groupPosition, int childPosition) 
    { 
        return childArray.get(groupPosition).get(childPosition); 
    } 
    public long getChildId(int groupPosition, int childPosition) 
    { 
        return childPosition; 
    } 
    public int getChildrenCount(int groupPosition) 
    { 
        return childArray.get(groupPosition).size(); 
    } 
    public View getChildView(int groupPosition, int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) 
    { 
        String string = childArray.get(groupPosition).get(childPosition); 
        return getGenericView(string); 
    } 
    // group method stub 
    public Object getGroup(int groupPosition) 
    { 
        return groupArray.get(groupPosition); 
    } 
    public int getGroupCount() 
    { 
        return groupArray.size(); 
    } 
    public long getGroupId(int groupPosition) 
    { 
        return groupPosition; 
    } 
    public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parent) 
    { 
        String string = groupArray.get(groupPosition); 
        return getGenericView(string); 
    } 
    // View stub to create Group/Children 's View 
    public TextView getGenericView(String string) 
    { 
        // Layout parameters for the ExpandableListView 
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( 
                ViewGroup.LayoutParams.FILL_PARENT, 64); 
        TextView text = new TextView(activity); 
        text.setLayoutParams(layoutParams); 
        // Center the text vertically 
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
        // Set the text starting position 
        text.setPadding(36, 0, 0, 0); 
        text.setText(string); 
        return text; 
    } 
    public boolean hasStableIds() 
    { 
        return false; 
    } 
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    { 
        return true; 
    } 
}

第五步:為控件掛上 Adapter

最後一步,透過 findViewById 取得前面宣告的 ExpandableListView,並將自訂的配接器設定給它,整個分組列表就完成囉。

復制代碼 代碼如下:ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView); 
expandableListView.setAdapter(new ExpandableAdapter(Main.this));

以上就是 ExpandableListView 的完整實作流程,希望對大家在 Android 開發上有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved