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