Skip to content

Commit

Permalink
added the position of the item that was interacted with to the callback
Browse files Browse the repository at this point in the history
changed the example app to better illustrate how to easily interact with SwipeDeck
  • Loading branch information
shrewduser committed Jan 6, 2016
1 parent 760f007 commit c2dcd34
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions app/src/main/java/com/daprlabs/swipedeck/SwipeDeckActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,31 @@ protected void onCreate(Bundle savedInstanceState) {
cardStack = (SwipeDeck) findViewById(R.id.swipe_deck);

final ArrayList<String> testData = new ArrayList<>();
testData.add("0");
testData.add("1");
testData.add("2");
testData.add("3");
testData.add("4");
testData.add("5");

final SwipeDeckAdapter adapter = new SwipeDeckAdapter(testData, this);
cardStack.setAdapter(adapter);

cardStack.setEventCallback(new SwipeDeck.SwipeEventCallback() {
@Override
public void cardSwipedLeft() {
Log.i("MainActivity", "card was swiped left");
public void cardSwipedLeft(int position) {
Log.i("MainActivity", "card was swiped left, position in adapter: " + position);
}

@Override
public void cardSwipedRight() {
Log.i("MainActivity", "card was swiped right");
public void cardSwipedRight(int position) {
Log.i("MainActivity", "card was swiped right, position in adapter: " + position);
}

@Override
public void cardClicked() {
Log.i("MainActivity", "card was clicked");
public void cardClicked(int position) {
Log.i("MainActivity", "card was clicked, position in adapter: " + position);
//example of how to get the item that was clicked / swiped / etc
Log.i("MainActivity", (String)adapter.getItem(position));
}

@Override
Expand All @@ -66,11 +68,10 @@ public void cardsDepleted() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testData.add("");
testData.add("a sample object. in this case a string.");
adapter.notifyDataSetChanged();
}
});

}

@Override
Expand Down
6 changes: 3 additions & 3 deletions cardstack/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/aaronbond/Swipe-Deck'
gitUrl = 'https://github.com/aaronbond/Swipe-Deck.git'

libraryVersion = '0.0.7'
libraryVersion = '0.1.0'

developerId = 'aaronbond'
developerName = 'Aaron Bond'
Expand All @@ -31,8 +31,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 7
versionName "0.0.7"
versionCode 8
versionName "0.1.0"
}
buildTypes {
release {
Expand Down
23 changes: 14 additions & 9 deletions cardstack/src/main/java/com/daprlabs/cardstack/SwipeDeck.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void addNextCard() {
nextAdapterCard++;
}
setZTranslations();
setupTopCard();
setupTopCard(nextAdapterCard);
}

private void setZTranslations() {
Expand All @@ -192,7 +192,6 @@ private void setZTranslations() {
}
}


/**
* Adds a view as a child view and takes care of measuring it
*
Expand Down Expand Up @@ -268,9 +267,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}


private void setupTopCard() {
private void setupTopCard(final int cardPosition) {
final View child = getChildAt(0);

//this calculation is to get the correct position in the adapter of the current top card
//the card position on setup top card is currently always the bottom card in the view
//at any given time.
int childCount = getChildCount();
final int pos = cardPosition - childCount;
int initialX = paddingLeft;
int initialY = paddingTop;

Expand All @@ -281,20 +285,20 @@ private void setupTopCard() {
@Override
public void cardSwipedLeft() {
removeTopCard();
if(eventCallback != null)eventCallback.cardSwipedLeft(pos);
addNextCard();
if(eventCallback != null)eventCallback.cardSwipedLeft();
}

@Override
public void cardSwipedRight() {
removeTopCard();
addNextCard();
if(eventCallback != null)eventCallback.cardSwipedRight();
if(eventCallback != null)eventCallback.cardSwipedRight(pos);
}

@Override
public void cardClicked() {
if(eventCallback != null)eventCallback.cardClicked();
if(eventCallback != null)eventCallback.cardClicked(pos);
}
}, initialX, initialY, ROTATION_DEGREES, OPACITY_END);

Expand Down Expand Up @@ -329,9 +333,10 @@ public void setRightImage(int imageResource){
}

public interface SwipeEventCallback {
void cardSwipedLeft();
void cardSwipedRight();
void cardClicked();
//returning the object position in the adapter
void cardSwipedLeft(int position);
void cardSwipedRight(int position);
void cardClicked(int position);
void cardsDepleted();
}

Expand Down

0 comments on commit c2dcd34

Please sign in to comment.