編輯:關於 Android 程式設計
在很多應用場景裡,單純的 ListView 無法滿足需求。當我們需要以「多組清單」的形式呈現資料時,就該請出 ExpandableListView 這個可展開的列表控制項了。它的作用就像把 ListView 進行分組——好比我們使用 QQ 時的「我的好友」「陌生人」「黑名單」,點一下展開,再點一下又收合回去。
ExpandableListView 是一個可垂直滾動、顯示兩級列表項的視圖。與 ListView 不同,它具有兩層結構,每一層都能被獨立展開並顯示其子項,而這些子項來自與該視圖關聯的 ExpandableListAdapter。
每個可展開的列表項旁都有一個指示符(箭頭),用以說明目前狀態(已展開、未展開、子項、最後一個子項等)。可以透過 setChildIndicator(Drawable) 與 setGroupIndicator(Drawable)(或對應的 XML 屬性)來自訂樣式,當然也可以沿用系統預設樣式。
和 ListView 一樣,ExpandableListView 也需要 Adapter 作為資料橋梁。適用的 Adapter 通常會繼承 BaseExpandableListAdapter,並重點覆寫 getGroupView 與 getChildView 這兩個方法。以下是幾個最常被使用的抽象方法:
[java]
// 取得指定分組、指定子項關聯的資料
public abstract Object getChild(int groupPosition, int childPosition);
// 取得子項的 View
public abstract View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent);
// 取得指定分組的子元素數量
public abstract int getChildrenCount(int groupPosition);
// 取得與指定分組關聯的資料
public abstract Object getGroup(int groupPosition);
// 取得分組總數
public abstract int getGroupCount();
// 取得分組的 View
public abstract View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent);
其中 getGroupView 只負責回傳分組的視圖,若想取得子元素的視圖,則需呼叫 getChildView;而 convertView 參數可用來重用舊的視圖物件,是提升效能的重要機制。
首先在 LinearLayout 中放入一個 ExpandableListView:
[xml]
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/myExpandableListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
接著在 Activity 中準備分組資料與子項資料,並套用自訂的 Adapter:
[java]
private List<String> GroupData;// 定義組資料
private List<List<String>> ChildrenData;// 定義組中的子資料
private void LoadListDate() {
GroupData = new ArrayList<String>();
GroupData.add("國家");
GroupData.add("人物");
GroupData.add("武器");
ChildrenData = new ArrayList<List<String>>();
List<String> Child1 = new ArrayList<String>();
Child1.add("蜀國"); Child1.add("魏國"); Child1.add("吳國");
ChildrenData.add(Child1);
// ……其餘分組依此類推
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LoadListDate();
ExpandableListView myExpandableListView =
(ExpandableListView) findViewById(R.id.myExpandableListView);
myExpandableListView.setAdapter(new ExpandableAdapter());
}
private class ExpandableAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return ChildrenData.get(groupPosition).get(childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView myText = null;
if (convertView != null) {
myText = (TextView) convertView;
myText.setText(ChildrenData.get(groupPosition).get(childPosition));
} else {
myText = createView(ChildrenData.get(groupPosition).get(childPosition));
}
return myText;
}
@Override
public int getChildrenCount(int groupPosition) {
return ChildrenData.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) { return GroupData.get(groupPosition); }
@Override
public int getGroupCount() { return GroupData.size(); }
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView myText = null;
if (convertView != null) {
myText = (TextView) convertView;
myText.setText(GroupData.get(groupPosition));
} else {
myText = createView(GroupData.get(groupPosition));
}
return myText;
}
@Override
public boolean hasStableIds() { return false; }
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
private TextView createView(String content) {
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 80);
TextView myText = new TextView(ExpandableListViewActivity.this);
myText.setLayoutParams(layoutParams);
myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
myText.setPadding(80, 0, 0, 0);
myText.setText(content);
return myText;
}
}
實務開發中常有各式客製需求,例如為每個 child 或 group 加上圖標、變換背景、預設展開某一組等。以下列幾個常用做法:
設定背景並預設展開第 n 組(n 從 0 起算):
[java]
myExpandableListView.setBackgroundResource(R.drawable.background);
myExpandableListView.expandGroup(0);
讓分組圖標隨展開/收合而改變,可在 res/drawable 下定義 Indicator.xml:
[xml]
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="@drawable/right" />
<item android:drawable="@drawable/down"></item>
</selector>
再於程式碼中套用:
[java]
myExpandableListView.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));
其他常用屬性還包括 android:childDivider(分隔子項的直線)、android:childIndicator(子項指示符)、android:groupIndicator(分組指示符)等。此外,也可以自訂 group.xml 或 child.xml 佈局檔,用 ImageView 搭配 TextView 打造更豐富的視覺呈現。
在 XML 佈局中,若 ExpandableListView 的上層視圖未嚴格定義大小,就不可對其 android:layout_height 使用 wrap_content。例如上層是 ScrollView 時便不建議這麼做,否則會在 setContentView 處拋出空指標錯誤。唯有當上層視圖具有明確尺寸(例如 100 像素)時,才能安全地使用 wrap_content。
最後補充一點:若某個 Activity 只需要一個 ExpandableListView,也可以直接繼承 ExpandableListActivity 來簡化開發。
Android 逆向工程常用工具入門指南(apktool 與 dex2jar)
寫在前面 整理這份筆記時,我偶爾也會自嘲是不是太無聊了,但既然項目告一段落,趁著手邊的鏈接還新鮮,乾脆一口氣寫完,省得日後再翻箱倒櫃。於是就有了這篇 Android 逆向
Android Studio 效率翻倍實戰技巧整理
本文整理自 Philippe Breault 的《Android Studio Tips Of the Day》系列,從中挑出實務上最能提升效率的部分。這些技巧一旦養成肌
Android 牛刀小試之"AA 算錢軟件"開發(三):換膚功能
今天主要給應用加了一個換膚功能——可以切換 6 種不同的背景,即點擊換膚按鈕時更換背景圖片。我在網上搜了好長時間,最終覺得最靠譜、最好理解的方案,是通過操作容器背景來實現
Android 線程中創建 Handler 的方法
理解 Handler 與線程的關係 Handler 是 Android 中常見的訊息處理工具,可將任務送入 MessageQueue,再由 Looper 取出執行。Han