thoughtbot / expandable-recycler-view

Custom Android RecyclerViewAdapters that collapse and expand
https://robots.thoughtbot.com/introducing-expandablerecyclerview
MIT License
2.12k stars 407 forks source link

toggleGroup does not work with groups off the screen #33

Closed vovakatz closed 7 years ago

vovakatz commented 7 years ago

I needed an expandable recyclerview which allows only one group to be expanded at the time. I added the follwoing code to GenreAdapter in your sample project:

`private ExpandableGroup expandedGroup; setOnGroupExpandCollapseListener(new GroupExpandCollapseListener() { @Override public void onGroupExpanded(ExpandableGroup group) { if (expandedGroup != null){ toggleGroup(expandedGroup); } expandedGroup = group; }

  @Override
  public void onGroupCollapsed(ExpandableGroup group) {

  }
});`

It works fine when the previously expanded group is visible on the screen. If the group is off the screen, it will not collapse when calling toggleGroup on it. Unless I am doing something wrong?

Looks like there is an infinite loop:

12-23 11:46:07.395 25187-25187/com.thoughtbot.expandablerecyclerview.sample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.thoughtbot.expandablerecyclerview.sample, PID: 25187 java.lang.StackOverflowError: stack size 8MB at com.thoughtbot.expandablerecyclerview.models.ExpandableListPosition.getRecycledOrCreate(ExpandableListPosition.java:115) at com.thoughtbot.expandablerecyclerview.models.ExpandableListPosition.obtain(ExpandableListPosition.java:101) at com.thoughtbot.expandablerecyclerview.models.ExpandableList.getUnflattenedPosition(ExpandableList.java:73) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.isGroupExpanded(ExpandCollapseController.java:63) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:85) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.expandGroup(ExpandCollapseController.java:44) at com.thoughtbot.expandablerecyclerview.ExpandCollapseController.toggleGroup(ExpandCollapseController.java:89) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.toggleGroup(ExpandableRecyclerViewAdapter.java:172) at com.thoughtbot.expandablerecyclerview.sample.expand.GenreAdapter$1.onGroupExpanded(GenreAdapter.java:25) at com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter.onGroupExpanded(ExpandableRecyclerViewAdapter.java:121) at com.thoughtbot.expandablerecyclerview.ExpandColl

mandybess commented 7 years ago

Hi @vovakatz! Just released an update which I believe might fix this. Let me know if you are still having issues!

washul commented 6 years ago

@mandybess hello, I have the same problem, I use the library 'com.thoughtbot:expandablerecyclerview:1.3' and com.thoughtbot:expandablecheckrecyclerview:1.4 and and the same bug, The methods I use are:

mAdapter.setOnGroupExpandCollapseListener(new GroupExpandCollapseListener() { @Override public void onGroupExpanded(ExpandableGroup group) {

            if (expandedGroup != null && !expandedGroup.equals(group) && mAdapter.isGroupExpanded(expandedGroup)){
                mAdapter.toggleGroup(expandedGroup);
            }
            expandedGroup = group;

        }

        @Override
        public void onGroupCollapsed(ExpandableGroup group) {

        }
    });

and I use a different one but the same problem, that @vovakatz

this is the other metod, i use and the same problem:

@Override public void onGroupExpanded(int positionStart, int itemCount) { if (itemCount > 0) { int groupIndex = expandableList.getUnflattenedPosition(positionStart).groupPos; notifyItemRangeInserted(positionStart, itemCount);

            for (ExpandableGroup grp : mGroups) {
                if (!grp.equals(mGroups.get(groupIndex))) {
                    Log.e("if ", String.valueOf(mGroups.get(groupIndex)));
                    if (this.isGroupExpanded(grp)) {
                        Log.e("notificacion ", String.valueOf(grp));
                        this.toggleGroup(grp);
                        this.notifyDataSetChanged();
                    }
                }
            }

    }

please cand you healpme?