Collapse all group except selected group in expandable listview android

余生颓废 提交于 2019-12-08 22:41:18

问题


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.


回答1:


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;
    }
});



回答2:


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;
    }
});



回答3:


@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;
    }



回答4:


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.




回答5:


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;
        }
    });



回答6:


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;
    }



回答7:


   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);
                    }
                }
            }
        });



回答8:


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);
            }
        });`


来源:https://stackoverflow.com/questions/17586174/collapse-all-group-except-selected-group-in-expandable-listview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!