ExpandableListView 是可以兩層展開的 ListView,可以用在兩層資料的呈現。
Extends Structure
- java.lang.Object
- android.view.View
- android.view.ViewGroup
- android.widget.AdapterView
- android.widget.AbsListView
- android.widget.ListView
- android.widget.ExpandableListView
XML Attributes
Attribute Name |
Related Method |
Description |
android:childDivider |
|
|
android:childIndicator |
|
|
android:childIndicatorEnd |
|
|
android:childIndicatorLeft |
|
|
android:childIndicatorRight |
|
|
android:childIndicatorStart |
|
|
android:groupIndicator |
|
|
android:indicatorEnd |
|
|
android:indicatorLeft |
|
|
android:indicatorRight |
|
|
android:indicatorStart |
|
|
Adapter
ExpandableListView 要使用 setAdapter(ExpandableListAdapter) 接資料
雖然 ExpandableListView 是繼承 ListView,不過當 ExpandableListView 執行 setAdapter(ListAdapter) 時,會出現 Error。
BaseExpandableListAdapter 說明:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
@Override public int getGroupCount() { return 0; }
@Override public int getChildrenCount(int groupPosition) { return 0; }
@Override public Object getGroup(int groupPosition) { return null; }
@Override public Object getChild(int groupPosition, int childPosition) { return null; }
@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 isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); convertView = inflater.inflate(R.layout.expandable_list_group_view, null); }
return convertView; }
@Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); convertView = inflater.inflate(R.layout.expandable_list_child_view, null); }
return convertView; }
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } }
|
How to use OnItemLongClickListener
在看文檔時,會發現它的 Listener 有新增 setOnChildClickListener 和 setOnGroupClickListener,可是就是沒有 LongClickListener。
事實上是有辦法解決的,一樣要用 OnItemLongClickListener
exListView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { int itemType = ExpandableListView.getPackedPositionType(id);
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { childPosition = ExpandableListView.getPackedPositionChild(id); groupPosition = ExpandableListView.getPackedPositionGroup(id);
return true; } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { groupPosition = ExpandableListView.getPackedPositionGroup(id);
return true; } else { return false; } } });
|
References