Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ExpandableListView - set certain group to non expandable

I have an ExpandableListView and I need to set a certain group (groupPosition is known and always the same) to be non-expandable and just start a new Activity when clicked. Is there a method to do that?

UPDATE

I also need only one group to be expanded at a time, and tried the following (but this is not working properly):

  getExpandableListView().setOnGroupClickListener(
            new OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent,
                        View v, int groupPosition, long id) {
                    if (groupPosition == 1) {
                        Toast.makeText(getApplicationContext(), "TEST",
                                Toast.LENGTH_SHORT).show();
                    } else if (groupPosition != lastExpandedGroupPosition) {
                        getExpandableListView().collapseGroup(
                                lastExpandedGroupPosition);
                    } else
                        getExpandableListView().expandGroup(groupPosition);
                    lastExpandedGroupPosition = groupPosition;

                    return true;
                }
            });
like image 851
Droidman Avatar asked Feb 07 '13 09:02

Droidman


2 Answers

This will do the trick - check in onGroupClick for the group you want to handle specially, return true for it, return false for any you want to be handled by the default action (expand/collapse.)

    mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            if(groupPosition == 2) {
                Toast.makeText(mContext, "Group 2 clicked", Toast.LENGTH_LONG).show();
                return true;
            } else
                return false;
        }
    });
like image 187
Clyde Avatar answered Oct 17 '22 07:10

Clyde


You can use OnGroupClickListener for ExpandableListView to restrict group clicks.

Updated

private int previousGroupPosition = -1;
private boolean isDoubleTap = false;    

expLv.setOnGroupClickListener(new OnGroupClickListener()
{
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
    {
        if(previousGroupPosition != -1)
            expLvTodoCategories.collapseGroup(previousGroupPosition);

        if(previousGroupPosition != groupPosition || isDoubleTap)
        {
            isDoubleTap = false;
            previousGroupPosition = groupPosition;
            if(groupPosition == 1)
            {
                 //Start Activity
            }
            else
            {
                expLv.expandGroup(groupPosition);
            }
        }
        else
            isDoubleTap = true;

        return true;
    }
});

Try the above code, It can be useful..

like image 2
Santhosh Avatar answered Oct 17 '22 09:10

Santhosh