我正在尝试创建一个ContextMenu
,如果用户长按一个ExpandableListAdater
的项目(组或组子项),但ContextMenu
仅在组项目上长按时显示,而不是在组子项上:
创建:
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
onCreateContextMenu:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Log.i("", "Click");
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
// Show context menu for groups
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
menu.setHeaderTitle("Group");
menu.add(0, 0, 1, "Delete");
// Show context menu for children
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
menu.setHeaderTitle("Child");
menu.add(0, 0, 1, "Delete");
}
}
onContextItemSelected:
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item
.getMenuInfo();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// do something with parent
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// do someting with child
}
return super.onContextItemSelected(item);
}
我觉得我错过了一些东西,因为如果我长按孩子的话,“Click”就不会被记录。我想 onCreateContextMenu
在我长按孩子时没有被调用。我该如何管理为 ExpandableListAdapter
组的子项展示一个 ContextMenu
?