Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide swiped position to onCardSwiped event #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Provide swiped position to onCardSwiped event
Whenever a card is swiped, this also captures the position
of the swiped card and emits it to the event. This allows
us to trust the position at the event handler, and not have
to inspect the cardLayoutManager directly. This is helpful
in the case where our event may trigger another runnable or
otherwise may not be able to trust that the layoutManager
has not already calculated another topPosition.
  • Loading branch information
Barryrowe committed Oct 28, 2019
commit b6a6f9096c232b4cd63e214d301b38ad2a578e7b
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -292,7 +292,7 @@ CardStackLayoutManager.setSwipeableMethod(SwipeableMethod.AutomaticAndManual)
| Method | Description |
| :---- | :---- |
| CardStackListener.onCardDragging(Direction direction, float ratio) | This method is called while the card is dragging. |
| CardStackListener.onCardSwiped(Direction direction) | This method is called when the card is swiped. |
| CardStackListener.onCardSwiped(Direction direction, int swipedPosition) | 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. |
@@ -317,7 +317,7 @@ CardStackLayoutManager.setSwipeableMethod(SwipeableMethod.AutomaticAndManual)
| :---- | :---- |
| CardStackView.CardEventListener | CardStackListener |
| onCardDragging(float percentX, float percentY) | onCardDragging(Direction direction, float ratio) |
| onCardSwiped(SwipeDirection direction) | onCardSwiped(Direction direction) |
| onCardSwiped(SwipeDirection direction) | onCardSwiped(Direction direction, int swipedPosition) |
| onCardReversed() | onCardRewound() |
| onCardMovedToOrigin() | onCardCanceled() |
| onCardClicked(int index) | This method is no longer provided. Please implement in your item of RecyclerView. |
Original file line number Diff line number Diff line change
@@ -270,6 +270,7 @@ private void update(RecyclerView.Recycler recycler) {
final Direction direction = state.getDirection();

state.next(state.status.toAnimatedStatus());
final int swipedPosition = state.topPosition;
state.topPosition++;
state.dx = 0;
state.dy = 0;
@@ -311,7 +312,7 @@ private void update(RecyclerView.Recycler recycler) {
new Handler().post(new Runnable() {
@Override
public void run() {
listener.onCardSwiped(direction);
listener.onCardSwiped(direction, swipedPosition);
View topView = getTopView();
if (topView != null) {
listener.onCardAppeared(getTopView(), state.topPosition);
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

public interface CardStackListener {
void onCardDragging(Direction direction, float ratio);
void onCardSwiped(Direction direction);
void onCardSwiped(Direction direction, int swipedPosition);
void onCardRewound();
void onCardCanceled();
void onCardAppeared(View view, int position);
@@ -14,7 +14,7 @@ public interface CardStackListener {
@Override
public void onCardDragging(Direction direction, float ratio) {}
@Override
public void onCardSwiped(Direction direction) {}
public void onCardSwiped(Direction direction, int swipedPosition) {}
@Override
public void onCardRewound() {}
@Override
Original file line number Diff line number Diff line change
@@ -45,9 +45,9 @@ class MainActivity : AppCompatActivity(), CardStackListener {
Log.d("CardStackView", "onCardDragging: d = ${direction.name}, r = $ratio")
}

override fun onCardSwiped(direction: Direction) {
Log.d("CardStackView", "onCardSwiped: p = ${manager.topPosition}, d = $direction")
if (manager.topPosition == adapter.itemCount - 5) {
override fun onCardSwiped(direction: Direction, swipedPosition: Int) {
Log.d("CardStackView", "onCardSwiped: p = $swipedPosition, d = $direction")
if (swipedPosition == adapter.itemCount - 5) {
paginate()
}
}