diff --git a/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java b/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java index 661bc5b1..e9419d82 100644 --- a/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java +++ b/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java @@ -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 diff --git a/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackDataObserver.java b/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackDataObserver.java index 8ec46c30..6805322b 100644 --- a/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackDataObserver.java +++ b/cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackDataObserver.java @@ -7,8 +7,6 @@ public class CardStackDataObserver extends RecyclerView.AdapterDataObserver { - public boolean isRegistered = false; - private final RecyclerView recyclerView; public CardStackDataObserver(RecyclerView recyclerView) {