Skip to content

Commit

Permalink
Merge pull request #164 from yuyakaido/issue/153
Browse files Browse the repository at this point in the history
Fix crash when recreating holder activity
  • Loading branch information
yuyakaido authored Dec 12, 2018
2 parents 34597ac + 03329b2 commit 7f60c99
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,12 @@
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.yuyakaido.android.cardstackview.internal.CardStackDataObserver;
import com.yuyakaido.android.cardstackview.internal.CardStackSnapHelper;

public class CardStackView extends RecyclerView {

private final AdapterDataObserver observer = new AdapterDataObserver() {
private CardStackLayoutManager getCardStackLayoutManager() {
LayoutManager manager = getLayoutManager();
if (manager instanceof CardStackLayoutManager) {
return (CardStackLayoutManager) manager;
}
throw new IllegalStateException("CardStackView must be set CardStackLayoutManager.");
}
@Override
public void onChanged() {
CardStackLayoutManager manager = getCardStackLayoutManager();
manager.setTopPosition(0);
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
// Do nothing
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
// Do nothing
}

@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
// Do nothing
}

@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
CardStackLayoutManager manager = getCardStackLayoutManager();
if (positionStart == 0) {
manager.setTopPosition(0);
}
manager.removeAllViews();
}

@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
// Do nothing
}
};
private final CardStackDataObserver observer = new CardStackDataObserver(this);

public CardStackView(Context context) {
this(context, null);
Expand Down Expand Up @@ -90,6 +49,7 @@ protected void onAttachedToWindow() {
RecyclerView.Adapter adapter = getAdapter();
if (adapter != null) {
adapter.registerAdapterDataObserver(observer);
observer.isRegistered = true;
}
}

Expand All @@ -98,7 +58,10 @@ protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
RecyclerView.Adapter adapter = getAdapter();
if (adapter != null) {
adapter.unregisterAdapterDataObserver(observer);
if (observer.isRegistered) {
adapter.unregisterAdapterDataObserver(observer);
observer.isRegistered = false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.yuyakaido.android.cardstackview.internal;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.yuyakaido.android.cardstackview.CardStackLayoutManager;

public class CardStackDataObserver extends RecyclerView.AdapterDataObserver {

public boolean isRegistered = false;

private final RecyclerView recyclerView;

public CardStackDataObserver(RecyclerView recyclerView) {
this.recyclerView = recyclerView;
}

@Override
public void onChanged() {
CardStackLayoutManager manager = getCardStackLayoutManager();
manager.setTopPosition(0);
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
// Do nothing
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
// Do nothing
}

@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
// Do nothing
}

@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
CardStackLayoutManager manager = getCardStackLayoutManager();
if (positionStart == 0) {
manager.setTopPosition(0);
}
manager.removeAllViews();
}

@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
// Do nothing
}

private CardStackLayoutManager getCardStackLayoutManager() {
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof CardStackLayoutManager) {
return (CardStackLayoutManager) manager;
}
throw new IllegalStateException("CardStackView must be set CardStackLayoutManager.");
}

}

0 comments on commit 7f60c99

Please sign in to comment.