Monday, April 27, 2015

Tinder swipe functionality with AndTinder

AndTinder provides a nice library which offers a Tinder like swipe functionality.

Since this lib didn't work from the beginning for me, I am writing this blog on my problems.

Main problem was, that in the documentation nothing was mentioned on how to use the onCardDimissedListener, which is being triggered right after a card is being liked/disliked.

Here is the code I used to initialize the adapter

// global variable
CardContainer mCardContainer;

...

// onCreateView function
mCardContainer = (CardContainer) view.findViewById(R.id.cardView);

List<CardModel> cardModelList = new ArrayList<>();

for (int i = 0; i < 10; i++) {
final CardModel cardModel = new CardModel();
cardModel.setTitle("Model " + i);
cardModel.setDescription("model " + i);

cardModel.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
@Override
public void onDislike() {
Log.e("Swipeable Card", "I did not like it -- " + cardModel.getTitle() + " - " + cardModel.getDescription());
}

@Override
public void onLike() {
Log.e("Swipeable Card", "I liked it -- " + cardModel.getTitle() + " - " + cardModel.getDescription());
}
});
cardModelList.add(cardModel);
}

SimpleCardStackAdapteradapter = new SimpleCardStackAdapter(getActivity(), cardModelList);
mCardContainer.setAdapter(adapter);


Everything was working fine, except for one problem, that the output in the Log was vice versa. This means, that Pic 10 was showed but in the log it did write Pic 1. And when Pic 9 was shown it did write Pic 2 in the log.

I had to change the source code in the following to make this work. For me this change has no negative side effect so far.

Open CardContainer.java file in the AndTinder project and go all the way down to the onFling() function.

Replace this line of code
CardModel cardModel = (CardModel)getAdapter().getItem(getChildCount() - 1);

by this line of code
CardModel cardModel = (CardModel)getAdapter().getItem(getAdapter().getCount() - getChildCount());

This is all you have to do.

Please comment if you like it or have further questions.

Greetings
Tobias

5 comments:

  1. Hi,

    I Solved these Issue in my Project. But Now I stuck in some new thing. And I think may be you can help me.

    If I have Stack of 10 Cards or any number of Cards, I Just want to show 1 Card at a time, there is one variable in Library mMaxVisible, I set it to my required Number, But after Swiping 1 or 2 times it Crashes my app.

    Please Guide with these Issue if you have any idea on these. It would be a big help.

    ReplyDelete
  2. Hi Tobias,

    I have same problem. Can you show your SimpleCardStackAdapter class code?

    ReplyDelete
  3. Hi i have another problem about swipe cards, i wanna cards reseverble when adapter is finished i have to update how can i do

    ReplyDelete
  4. Great post....Thank you for posting the great content……I found it quiet interesting, hopefully you will keep posting such blogs…
    If you Want more details kindly Visit us

    ReplyDelete