質問

I'm trying to create a ContextMenu if the user long clicks on an item (group or group child) for an ExpandableListAdater but the ContextMenu only shows for long clicks on group items and not for group child items:

onCreate:

@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);
}

I think I'm missing someting because "Click" isn't logged if I long click on a child. I think that onCreateContextMenu isn't invoked if I long click on a child. How do I manage to show a ContextMenu for ExpandableListAdapter group children?

役に立ちましたか?

解決

The solution is:

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // Check your child items here
    return true;
}

Auto-implementation was return false;

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top