編輯:Android 開發實例
ExpandableListView 是 Android 內建的「手風琴」列表控件,支援兩層級的資料結構:分組(Group)與子項(Child)。點一下標題展開內容,再點一下收起,非常適合設定頁、分類目錄、通訊錄分組這類需要階層呈現的介面。
這是一個垂直滾動的檢視,用來顯示兩個級別的列表項目,資料來源為 ExpandableListAdapter。每個分組可以獨立展開或收合。
ExpandableListContextMenuInfo menuInfo =
(ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) menuInfo.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView
.getPackedPositionGroup(menuInfo.packedPosition);
int childPos = ExpandableListView
.getPackedPositionChild(menuInfo.packedPosition);
}
這是一個介面,負責把底層資料連結到 ExpandableListView。實作這個介面之後,就能提供「依分組分類的子項資料」,並負責產生各個子項與分組的檢視。
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// 範例資料:children[i] 存放 groups[i] 底下的子項字串陣列
public String[] groups = {
"我的好友", "新疆同學", "親戚", "同事"
};
public String[][] children = {
{ "胡算林", "張俊峰", "王志軍", "二人" },
{ "李秀婷", "蔡喬", "別高", "余音" },
{ "攤派新", "張愛明" },
{ "馬超", "司道光" }
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// ExpandableListView 的版面參數
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListDemo.this);
textView.setLayoutParams(lp);
// 文字垂直置中
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// 設定文字起始內距
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
手風琴列表的核心在於「兩層資料的對齊」:getGroupCount() 決定有幾個攤開的標題,getChildrenCount(groupPosition) 決定每個標題底下有幾個子項,而 getChildView 與 getGroupView 分別負責兩種層級的畫面繪製。範例中把清單抽成 getGenericView(),讓分組與子項共用同一套版面參數,是減少重複程式碼的實用小技巧。
Android 多媒體播放:MediaPlayer 控制音樂
Android 的 MediaPlayer 類別可以控制音訊與影片播放。開發者可以透過它實作播放、暫停、快轉、倒退與音量控制等功能。 建立 MediaPlayer 將音樂
Android 筆記:自製下拉刷新 ListView 的完整拆解
ListView 是 Android 裡應用最廣的元件之一,功能強大、擴充彈性高,前面幾篇我們陸續玩過分組、拖曳排序、3D 立體、游標、圓角等變形。這次要動手做的是另一個
第三方應用接入微信平台研究分享(一):SDK 結構與通信流程
微信平台開放之後,不少第三方應用都想接進來——畢竟能借用微信建立起來的人際鏈來擴展自己的應用,確實比自己從零經營社交省下可觀的成本。由於實習需要,我最近也在研究這塊,不過
Android 應用國際化入門:依地區切換字串資源
國際化應用的第一步,是把畫面文字從程式碼中抽出,改放進資源檔。這樣一來,新增語言時不必修改程式邏輯。 資源資料夾命名 Android 使用 values 加上語言代碼的方