Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active December 12, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/4721090 to your computer and use it in GitHub Desktop.
Save daichan4649/4721090 to your computer and use it in GitHub Desktop.
ExpandableListView 長押し処理実装方法 (for Android)
// ExpandableListView 長押し時処理実装方法
// イベント発生順番
// (1) OnItemLongClickListener#onItemLongClick
// (2) OnCreateContextMenuListener#onCreateContextMenu
expandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// 選択要素位置
int groupPosition = ExpandableListView.getPackedPositionGroup(id);
int childPosition = ExpandableListView.getPackedPositionChild(id);
int packedPositionType = ExpandableListView.getPackedPositionType(id);
if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// group
} else if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_CHILDP) {
// child
}
return false;
}
});
expandableListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// 選択要素位置
long packedPosition = ((ExpandableListContextMenuInfo) menuInfo).packedPosition;
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
int packedPositionType = ExpandableListView.getPackedPositionType(packedPosition);
if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUPD) {
// group
} else if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// child
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment