Programing

확장 가능한 ListView Android에서 선택한 그룹을 제외한 모든 그룹 축소

crosscheck 2020. 11. 29. 09:54
반응형

확장 가능한 ListView Android에서 선택한 그룹을 제외한 모든 그룹 축소


확장 가능한 목록보기를 사용하여 Android 애플리케이션을 개발 중입니다. 실제로 필요한 것은 자식이 포함 된 그룹을 나열하는 것입니다.

확장 할 수없는 그룹을 선택하면 확장되어야합니다. 이때 두 번째 그룹을 선택하면 첫 번째 그룹이 축소되어야합니다. Google을 사용했지만 원하는 것을 찾을 수 없습니다. 제발 도와주세요.


현재 확장 된 그룹 위치를 변수에 저장합니다. onGroupExpanded에서 다음을 수행하십시오.

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

이 코드를 사용하면 작동합니다.

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

ExpandableListView에 3 개 이상의 그룹이있는 경우 :

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

클릭 한 그룹을 제외한 모든 그룹을 축소합니다.


취득 ExpendableListView후 다음 메소드를 오버라이드 (override) -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);
            }
        });`

참고URL : https://stackoverflow.com/questions/17586174/collapse-all-group-except-selected-group-in-expandable-listview-android

반응형