編輯:關於 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);
}
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;
}
}
最後一步,透過 findViewById 取得前面宣告的 ExpandableListView,並將自訂的配接器設定給它,整個分組列表就完成囉。
復制代碼 代碼如下:ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(Main.this));
以上就是 ExpandableListView 的完整實作流程,希望對大家在 Android 開發上有所幫助。
Android + JSON + PHP + MySQL 打造用戶反饋功能全流程
幾乎每一個上線項目都離不開「意見反饋」這個入口。它的實現思路其實不複雜:客戶端收集用戶填寫的內容,以 POST 方式提交給服務端,服務端寫入資料庫並回傳一個 JSON 結
Android 通訊錄開發實戰(二):讀取並呈現通話記錄
在上一篇我們已經完成了聯繫人資料的讀取,本篇接著談談如何取得並展示手機的通話記錄。整體思路與聯繫人類似,透過系統的 CallLog 內容提供者查詢資料,再用自訂的 Ada
風雲直播怎麼關閉彈幕?兩步取消滿螢幕飄字
看直播時彈幕有兩面:熱鬧的時候很帶氣氛,人少或內容雜的時候就只剩遮擋畫面。風雲直播的彈幕預設開啟,關閉方式其實很簡單,藏在「我」的頁面裡。第一步:開啟風雲直播,點擊首頁右
Android Studio 2.1 快速接入第三方短信驗證 SDK 實作指南
前言 初次整合第三方類庫時,常因流程不熟而卡關。本文以 Android Studio 2.1 為例,示範如何快速完成短信驗證碼功能。 1. 註冊 Mob 帳號 前往 Mo