Skip to content

Commit

Permalink
Merge pull request #173 from yuyakaido/issue/167
Browse files Browse the repository at this point in the history
Fix crash when unregistering AdapterDataObserver
  • Loading branch information
yuyakaido authored Dec 21, 2018
2 parents f3cade7 + 62b01c2 commit 0ecd57b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,14 @@ public void setAdapter(Adapter adapter) {
if (getLayoutManager() == null) {
setLayoutManager(new CardStackLayoutManager(getContext()));
}
super.setAdapter(adapter);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
RecyclerView.Adapter adapter = getAdapter();
if (adapter != null) {
adapter.registerAdapterDataObserver(observer);
observer.isRegistered = true;
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
RecyclerView.Adapter adapter = getAdapter();
if (adapter != null) {
if (observer.isRegistered) {
adapter.unregisterAdapterDataObserver(observer);
observer.isRegistered = false;
}
// Imitate RecyclerView's implementation
// http://tools.oesf.biz/android-9.0.0_r1.0/xref/frameworks/base/core/java/com/android/internal/widget/RecyclerView.java#1005
if (getAdapter() != null) {
getAdapter().unregisterAdapterDataObserver(observer);
getAdapter().onDetachedFromRecyclerView(this);
}
adapter.registerAdapterDataObserver(observer);
super.setAdapter(adapter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

public class CardStackDataObserver extends RecyclerView.AdapterDataObserver {

public boolean isRegistered = false;

private final RecyclerView recyclerView;

public CardStackDataObserver(RecyclerView recyclerView) {
Expand Down

0 comments on commit 0ecd57b

Please sign in to comment.