Search code examples
javaandroidandroid-studioexpandablelistviewonitemlongclicklistener

Block expanding of Expandable List View on long click


I am developing an android application, in which I am using Expandablelistview to show some data. Currently the list view will expand on both clicking on group and on releasing after long click. But, I need to prevent from expanding the Expandablelistview on long click.

My code segment looks like this:

elvItemList = (ExpandableListView) root.findViewById(R.id.elv_item_list);
elvItemList.setOnGroupClickListener(this);
elvItemList.setAdapter(smListAdapter);
elvItemList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            Utils.logit("SMLOG", "Long button pressed");
            //
        }
        return false;
    }
});

can any one help me?


Solution

  • ExpandableListView.PACKED_POSITION_TYPE_GROUP is the id of a group, change it to ExpandableListView.PACKED_POSITION_TYPE_CHILD and you can manipulate with longclicks on group childs.

    Something like that:

    elvItemList.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    // Your code with group long click
    return true;
    }
    return false;
    }
    });