質問

I'm developing android application using expandable list view. Actually what I need is, I'm listing group, which contains child.

If I select an unexpandable group, it should expand, after I ll select second group at that time the first group should be collapsed. I did Google, but I couldn't find what I want. Please help me out.

役に立ちましたか?

解決

Have the current expanded group position stored in a variable. In onGroupExpanded do the following.

private int lastExpandedPosition = -1;
private ExpandableListView lv; //your expandable listview
...

lv.setOnGroupExpandListener(new OnGroupExpandListener() {

    @Override
    public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                lv.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
    }
});

他のヒント

Use this code this will work

expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
    int previousItem = -1;

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

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

If the ExpandableListView has more than 2 groups:

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        for (int g = 0; g < expandableListAdapter.getGroupCount(); g++) {
            if (g != groupPosition) {
                expandableListView.collapseGroup(g);
            }
        }
    }
});    

It will collapse all groups except the clicked one.

Get ExpendableListView and then override the following method - setOnGroupExpandListener

expandableListView = (ExpandableListView) findViewById(R.id.exp_listview);

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousItem = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousItem)
                expandableListView.collapseGroup(previousItem);
            previousItem = groupPosition;
        }
    });

Try putting this in your ExpandableListAdapter, listView is a reference to the ExpandableListView itself. And lastExpandedGroupPosition is a integer member variable defined inside your ExpandableListAdapter.

 @Override
    public void onGroupExpanded(int groupPosition)

{

//collapse the old expanded group, if not the same

//as new group to expand

if(groupPosition != lastExpandedGroupPosition)

{

  listView.collapseGroup(lastExpandedGroupPosition);

  }

        super.onGroupExpanded(groupPosition);           
        lastExpandedGroupPosition = groupPosition;
    }
   elstListView1.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            for(int i=0;i<listDataHeader.size();i++){
                if(i==groupPosition){
                    //do nothing}
                    else{
                        elstListView1.collapseGroup(i);
                    }
                }
            }
        });

I also faced the same problem. But I could solved my problem by the following code:

As I had 4 Headers in my expandable list, based on that i wrote like this

expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPos) {
                // TODO Auto-generated method stub
                expListView.collapseGroup(groupPos+1);
                expListView.collapseGroup(groupPos-1);
                expListView.collapseGroup(groupPos-2);
                expListView.collapseGroup(groupPos+2);
                expListView.collapseGroup(groupPos+3);
                expListView.collapseGroup(groupPos-3);
            }
        });`
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top