Rakshi
Rakshi

Reputation: 6856

Expanded listview required to expand the row one at a time

im using expanded listview in my project, i need to expand the listview only one at a time, ie i expand a item and when try to expand another item in the list the previous item which i expanded has to collapse and the new item clicked has to expand, Answers will be greatly appreciated, i'm trying with the below snippet for the on click listener..

 convertViewpar.setOnClickListener(new OnClickListener() {

    int i=1;
    public void onClick(View v) {
    i = i++;
    int[] expds = new int[100];
    expds[0]=0;
    expds[i] = groupPosition;
    if(expds[i]==expds[i-1]){
    if(isExpanded)
    expandlist.collapseGroup(i);
    else
    expandlist.expandGroup(i);
    }else{
    expandlist.collapseGroup(i-1);
     if(isExpanded)
        expandlist.collapseGroup(i);
    else
    expandlist.expandGroup(i);

    }


}
}); 

Upvotes: 4

Views: 7304

Answers (2)

Anil Jadhav
Anil Jadhav

Reputation: 2158

Add implements OnGroupExpandListener at class level and in onCreate Method

    listView.setOnGroupExpandListener(this);

and add this method

/*
 * (non-Javadoc)
 * 
 * @see
 * android.widget.ExpandableListView.OnGroupExpandListener#onGroupExpand
 * (int)
 */
public void onGroupExpand(int groupPosition) {
    int len = expListAdapter.getGroupCount();

    for (int i = 0; i < len; i++) {
        if (i != groupPosition) {
            listView.collapseGroup(i);
        }
    }
}

It will works definitely.

Upvotes: 25

Andro Selva
Andro Selva

Reputation: 54322

Try this solution. use a GroupClcik Listener and check which position is currently clicked and collpase all other groups,

 expList.setOnGroupClickListener(new OnGroupClickListener() {

        public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2,
                long arg3) {
            int count =  yourAdapter.getGroupCount();
            for (int i = 0; i <count ; i++)
              if(arg2!=i)
                exp.collapseGroup(i);
              else
                  exp.expandGroup(i);
            return false;
        }
    });

Or as per this solution, Programmatically collapse a group in ExpandableListView,

@Override
public void onGroupExpanded(int groupPosition){
    //collapse the old expanded group, if not the same
    //as new group to expand
    if(groupPosition != lastExpandedGroupPosition){
        accordion.collapseGroup(lastExpandedGroupPosition);
    }

    super.onGroupExpanded(groupPosition);           
    lastExpandedGroupPosition = groupPosition;
}

Upvotes: 3

Related Questions