thoughtbot / expandable-recycler-view

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

Unique expanded list required #113

Closed bobcripps closed 7 years ago

bobcripps commented 7 years ago

I have a requirement for implementing a nested list view and I can pretty much lift the basics of the code in the sample. However in common with this requirement: https://github.com/thoughtbot/expandable-recycler-view/issues/25 I need to be able to automatically close an expanded view when another one is opened. The code below which I lifted from the aforementioned post does just that: However as per the sample I need a rotating ">" image and I can't see any way to get the GroupViewHolder derived class (GenreViewHolder in the sample) to get a notification that it needs to rotate the ">" which currently it can only get from a "click" hence the ">" is left in the up position when the group is collapsed by toggleGroup(). Any help greatly appreciated

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

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

  @Override
  public void onGroupCollapsed(ExpandableGroup group) {

  }
});
mandybess commented 7 years ago

Line 36 of ExpandActivity in the sample app is intentionally disabling the animation to the GenreViewHolder. Here is the full code from that line with the comment explaining why:

    // RecyclerView has some built in animations to it, using the DefaultItemAnimator.
    // Specifically when you call notifyItemChanged() it does a fade animation for the changing
    // of the data in the ViewHolder. If you would like to disable this you can use the following:
    RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
    if (animator instanceof DefaultItemAnimator) {
      ((DefaultItemAnimator) animator).setSupportsChangeAnimations(false);
    }

If you delete the setSupportsChangeAnimations line the rotation animation will work.

When callingadapter.toggle() the adapter passes that call through to the ExpandCollapseController who in turns calls through to the ExpandCollapseListener which the ExpandableRecyclerViewAdapter implements. If you look at theonGroupCollapsed() implementation in ExpandableRecyclerViewAdapter you can see that it calls notifyItemChanged on the header - which forces a call to onBindViewHolder which handles calling either expand or collapse on the GroupViewHolder. It's admittedly a little confusing to follow but this solution ensures both click events and programatic toggles will lead to the expand() and collapse() functions to be called.

bobcripps commented 7 years ago

Ok thanks @mandybess it now works but only after I changed the sample compile version to 1.4 so it might be an idea to update the sample gradle. I looked at the library logs and saw the fix. So anyone requiring this behaviour should implement the listener implement the comment above and make sure the library version is >= 1.4 in the gradle