編輯:關於 Android 程式設計
ExpandableListView 是一個垂直滾動、可顯示兩個層級(Child 子項、Group 分組)列表項的視圖,其列表項來自 ExpandableListAdapter,每個分組都能各自展開或收合。
1. 重要方法
expandGroup(int groupPos):展開指定的分組。
setSelectedGroup(int groupPosition):選取指定的分組。
setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup):選取指定的子項。
getPackedPositionGroup(long packedPosition):回傳所選的分組位置。
getPackedPositionForChild(int groupPosition, int childPosition):回傳所選子項的打包位置。
getPackedPositionType(long packedPosition):回傳所選項目的類型(Child 或 Group)。
isGroupExpanded(int groupPosition):判斷該分組是否已展開。
ExpandableListAdapter 是一個介面,用來把底層資料鏈接到 ExpandableListView,其實作會提供存取子項資料(依分組分類)的能力,並負責實例化子項與分組。
1. 重要方法
getChildId(int groupPosition, int childPosition):取得指定分組下指定子項相關的資料 id。
getChildrenCount(int groupPosition):回傳指定 Group 下的子項數量。
自訂的 Adapter 如下:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private List groupData;
private List> childrenData;
private Context context;
public MyExpandableListAdapter(Context context, List groupData, List> childrenData) {
this.context = context;
this.groupData = groupData;
this.childrenData = childrenData;
}
@Override
public int getGroupCount() {
return groupData.size();
}
@Override
public int getChildrenCount(int i) {
return childrenData.get(i).size();
}
@Override
public Object getGroup(int i) {
return groupData.get(i);
}
@Override
public Object getChild(int i, int i1) {
return childrenData.get(i).get(i1);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_head, null);
groupHolder = new GroupHolder();
groupHolder.txt = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) convertView.getTag();
}
groupHolder.txt.setText(groupData.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ItemHolder itemHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_child, null);
itemHolder = new ItemHolder();
itemHolder.tv_mes = (TextView) convertView.findViewById(R.id.tv_mes);
convertView.setTag(itemHolder);
} else {
itemHolder = (ItemHolder) convertView.getTag();
}
itemHolder.tv_mes.setText(childrenData.get(groupPosition).get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class GroupHolder {
public TextView txt;
}
class ItemHolder {
public TextView tv_mes;
}
}
public class MainActivity extends Activity implements ExpandableListView.OnChildClickListener {
private List groupArray;
private List> childArray;
private ExpandableListView expandableListView;
private MyExpandableListAdapter mMyExpandableListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
groupArray = new ArrayList<>();
groupArray.add("A");
groupArray.add("B");
groupArray.add("C");
List item_list1 = new ArrayList<>();
item_list1.add("1");
item_list1.add("2");
item_list1.add("3");
List item_list2 = new ArrayList<>();
item_list2.add("4");
item_list2.add("5");
item_list2.add("6");
List item_list3 = new ArrayList<>();
item_list3.add("7");
item_list3.add("8");
item_list3.add("9");
childArray = new ArrayList<>();
childArray.add(item_list1);
childArray.add(item_list2);
childArray.add(item_list3);
mMyExpandableListAdapter = new MyExpandableListAdapter(MainActivity.this, groupArray, childArray);
expandableListView.setAdapter(mMyExpandableListAdapter);
for (int i = 0; i < mMyExpandableListAdapter.getGroupCount(); i++) {
expandableListView.expandGroup(i); // 展開所有父分組
}
}
private void initView() {
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setOnChildClickListener(this);
}
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
Toast.makeText(MainActivity.this, "點擊了" + childArray.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();
return true;
}
}
效果如下圖:
我們可能會直覺地想到用 onItemLongClick,如下:
@Override
public boolean onItemLongClick(AdapterView> arg0, View view, int pos, long id) {
// pos 無法直接判斷父項或子項,詳見下文
return false;
}
這個方法用在 ListView 的長按事件中剛剛好,但在 ExpandableListView 中,第三個參數 pos 無法區分點擊的是父項還是子項,也無法判斷是哪個父項或子項。
在 ExpandableListView 回應的 onItemLongClick 方法中,pos 參數的值是:從上到下、父項加上已展開的子項,直到點擊位置所在的數目(注意只計已展開的,隱藏的子項不包括,且從 0 開始)。例如:父項 1(隱藏 3 個子項)、父項 2、子項 2-0、子項 2-1、子項 2-2,長按子項 2-1 時,pos 值為 3。顯然單靠 pos 無法確定點擊的是哪個子項或父項。
那麼,有沒有辦法直接取得 groupPos 與 childPos 呢?以下介紹幾種做法:
方法一:透過 getExpandableListPosition 取得打包位置
@Override
public boolean onItemLongClick(AdapterView> arg0, View view, int pos, long id) {
long packedPosition = mExpandableListView.getExpandableListPosition(pos);
int itemType = ExpandableListView.getPackedPositionType(packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// 根據 groupPosition 及 childPosition 判斷長按的是哪個子項,並做相應處理
} else {
// 根據 groupPosition 判斷長按的是哪個父項,並做相應處理(彈框等)
}
return false;
}
其底層原理與我們高中學的數學找規律類似:透過二分搜尋判斷點擊的 child 位於第幾組、第幾個。
方法二:透過 id 判斷位置
getExpandableListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPosition = ExpandableListView.getPackedPositionGroup(id);
int childPosition = ExpandableListView.getPackedPositionChild(id);
// 此時已取得 groupPosition 與 childPosition,可加入你的邏輯
return true; // 表示已處理該事件
}
return false;
}
});
方法三:使用上下文選單實現長按事件
透過 registerForContextMenu(downElv) 註冊上下文選單,並重寫以下兩個方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
// 該物件提供了選中項目的附加資訊
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int group = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int child = ExpandableListView.getPackedPositionChild(info.packedPosition);
if (type == 0) {
// 分組長按事件
} else if (type == 1) {
// 長按子項事件,可自訂 Dialog 或以 menu.add(...) 加入選單項
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
方法四:透過 setTag 綁定位置
看到 onItemLongClick 的第二個參數 view,它對應你點擊位置的 view。view 有個 getTag(int key) 方法,如果在建立該 view 時就透過 setTag(int key, Object value) 把 groupPos、childPos 存進去,那麼在回應 onItemLongClick 時就能直接取出使用。這也是必須自訂 BaseExpandableListAdapter 的理由——只有控制 view 的建立過程,才能把位置資訊綁進去:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView childTv = mkChildView();
// 標記位置:必須使用資源 Id 當 key(否則會出現執行時異常)
childTv.setTag(R.id.xxx01, groupPosition);
childTv.setTag(R.id.xxx02, childPosition);
return childTv;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView groupTv = mkGroupView();
groupTv.setTag(R.id.xxx01, groupPosition);
groupTv.setTag(R.id.xxx02, -1); // 設為 -1 表示長按的是父項,方便判斷
groupTv.setText(groups[groupPosition]);
return groupTv;
}
透過這個 ExpandableListView,我也看到了自己不少不足之處,仍需多加努力。
Android 如何導入外部預建數據庫
當 App 需要處理大量數據時,通常會把數據放進數據庫,再透過查詢與修改操作來管理。多數情況下我們在代碼裡即時建庫建表,但也有另一種場景:數據庫本身已經在外部做好並錄入完
全景圖像拼接(二):OpenCV stitching_detail 源碼全解析
上一篇我們跑通了 OpenCV 自帶的全景拼接示例,這一節深入 stitching_detail 的源碼,看看從一堆散亂照片到一張完整全景圖,中間到底發生了什麼。一、整體
Android MVP 架構實戰解析:從觀念到程式碼的完整落地
前言:為什麼要認真談 MVP MVP 這幾年越來越流行,被認可的程度也不斷提高。尤其在多人協作、需要把功能模組拆開分頭開發的專案裡,MVP 的優勢特別明顯。這篇文章就用最
啟動動畫實作:告別生硬冷啟動
所謂冷啟動(Cold Start),指的是應用被完全關閉後重新開啟、系統直接拋出首屏的那一瞬間。畫面「啪」地出現,用戶往往還沒反應過來。與其如此,不如在啟動時鋪一段簡短的