Skip to content

Commit

Permalink
Merge pull request #163 from yuyakaido/issue/140
Browse files Browse the repository at this point in the history
Add callback methods to notify appeared/disappeared card
  • Loading branch information
yuyakaido authored Dec 13, 2018
2 parents b05ae10 + f5aad81 commit 9a5cffe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ CardStackLayoutManager.setCanScrollVertical(true);
| CardStackListener.onCardSwiped(Direction direction) | This method is called when the card is swiped. |
| CardStackListener.onCardRewound() | This method is called when the card is rewinded. |
| CardStackListener.onCardCanceled() | This method is called when the card is dragged less than threshold. |
| CardStackListener.onCardAppeared(View view, int position) | This method is called when the card appeared. |
| CardStackListener.onCardDisappeared(View view, int position) | This method is called when the card disappeared. |

# Migration Guide

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ public RecyclerView.LayoutParams generateDefaultLayoutParams() {
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State s) {
update(recycler);
if (s.didStructureChange()) {
listener.onCardAppeared(getTopView(), state.topPosition);
}
}

@Override
Expand Down Expand Up @@ -179,6 +182,7 @@ private void update(RecyclerView.Recycler recycler) {
@Override
public void run() {
listener.onCardSwiped(direction);
listener.onCardAppeared(getTopView(), state.topPosition);
}
});
state.dx = 0;
Expand Down Expand Up @@ -397,6 +401,8 @@ private void smoothScrollToNext(int position) {
}

private void smoothScrollToPrevious(int position) {
listener.onCardDisappeared(getTopView(), state.topPosition);

state.proportion = 0.0f;
state.targetPosition = position;
state.topPosition--;
Expand All @@ -405,6 +411,10 @@ private void smoothScrollToPrevious(int position) {
startSmoothScroll(scroller);
}

public View getTopView() {
return findViewByPosition(state.topPosition);
}

public int getTopPosition() {
return state.topPosition;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.yuyakaido.android.cardstackview;

import android.view.View;

public interface CardStackListener {
void onCardDragging(Direction direction, float ratio);
void onCardSwiped(Direction direction);
void onCardRewound();
void onCardCanceled();
void onCardAppeared(View view, int position);
void onCardDisappeared(View view, int position);

CardStackListener DEFAULT = new CardStackListener() {
@Override
Expand All @@ -15,5 +19,9 @@ public void onCardSwiped(Direction direction) {}
public void onCardRewound() {}
@Override
public void onCardCanceled() {}
@Override
public void onCardAppeared(View view, int position) {}
@Override
public void onCardDisappeared(View view, int position) {}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,19 @@ protected void onTargetFound(View targetView, RecyclerView.State state, Action a

@Override
protected void onStart() {
CardStackListener listener = manager.getCardStackListener();
CardStackState state = manager.getCardStackState();
switch (type) {
case AutomaticSwipe:
state.next(CardStackState.Status.PrepareSwipeAnimation);
listener.onCardDisappeared(manager.getTopView(), manager.getTopPosition());
break;
case AutomaticRewind:
state.next(CardStackState.Status.RewindAnimating);
break;
case ManualSwipe:
state.next(CardStackState.Status.PrepareSwipeAnimation);
listener.onCardDisappeared(manager.getTopView(), manager.getTopPosition());
break;
case ManualCancel:
state.next(CardStackState.Status.RewindAnimating);
Expand All @@ -115,6 +118,7 @@ protected void onStop() {
break;
case AutomaticRewind:
listener.onCardRewound();
listener.onCardAppeared(manager.getTopView(), manager.getTopPosition());
break;
case ManualSwipe:
// Notify callback from CardStackLayoutManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import com.yuyakaido.android.cardstackview.CardStackLayoutManager;
import com.yuyakaido.android.cardstackview.CardStackListener;
Expand Down Expand Up @@ -75,6 +76,18 @@ public void onCardCanceled() {
Log.d("CardStackView", "onCardCanceled:" + manager.getTopPosition());
}

@Override
public void onCardAppeared(View view, int position) {
TextView textView = view.findViewById(R.id.item_name);
Log.d("CardStackView", "onCardAppeared: (" + position + ") " + textView.getText());
}

@Override
public void onCardDisappeared(View view, int position) {
TextView textView = view.findViewById(R.id.item_name);
Log.d("CardStackView", "onCardDisappeared: (" + position + ") " + textView.getText());
}

private void setupNavigation() {
// Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
Expand Down

0 comments on commit 9a5cffe

Please sign in to comment.