How to select ExpandableListView group item in MultiChoiceMode

712 Views Asked by At

In my project, I have an ExpandableListView and I set setMultiChoiceModeListener as shown below

elvItemList = (ExpandableListView) root.findViewById(R.id.elv_item_list);
elvItemList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
elvItemList.setMultiChoiceModeListener(new SMMultiChoiceModeListener());

the implementation of SMMultiChoiceModeListener is as follows:

private class SMMultiChoiceModeListener implements AbsListView.MultiChoiceModeListener {
    @Override
    public void onItemCheckedStateChanged(ActionMode mode,
                                          int position, long id, boolean checked) {
        final int checkedCount = elvItemList.getCheckedItemCount();
        selectEnable = true;
        mode.setSubtitle("" + checkedCount + " items selected");
        smListAdapter.checkedItems.put(position, checked);
        smListAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        MenuInflater inflater = activity.getMenuInflater();
        inflater.inflate(R.menu.selection_menu, menu);
        actionMode.setTitle("Select Items");
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_set_favorite:
                Toast.makeText(activity, "set favorite " + elvItemList.getCheckedItemCount() +
                        " items", Toast.LENGTH_SHORT).show();
                actionMode.finish();
                break;
            default:
                Toast.makeText(activity, "Clicked " + menuItem.getTitle(),
                        Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        smListAdapter.checkedItems.clear();
        smListAdapter.notifyDataSetChanged();
    }
}

I also implemented onGroupClickListener as

public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
    if(selectEnable) {
        Utils.logit("SMLOG", "Clicking");
        return true;
    }
    return false;
}

here selectEnable variable is used to block expanding once a ItemLongClick event fired.

Problem: When ever I fire an ItemLongClick the action bar will appear on the top, and shows message. After that group expansion will be blocked(This is expected). I need to select group item on itemClick but it's not working in my case. Can any one help me?

1

There are 1 best solutions below

0
On

Implement OnGroupClickListener as

   @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        elvItemList.setItemChecked(i, !elvItemList.isItemChecked(i));
        return selectEnable;
    }

repalce the 2nd line in onItemCheckedStateChanged to

selectEnable = (checkedCount != 0);

Highlight color may be changed in getGroupView