kikoso / Swipeable-Cards

A native library providing a Tinder-like cards effect. A card can be constructed using an image and displayed with animation effects, dismiss-to-like and dismiss-to-unlike, and use different sorting mechanisms.
Apache License 2.0
1.48k stars 364 forks source link

Endless adapter #39

Open franciscossb opened 9 years ago

franciscossb commented 9 years ago

Does anyone know a way to always show cards? how can I add a card to the bottom of the stack when I dismiss one?

Thank you

TiagoGouvea commented 9 years ago

I'm looking for this solution too.

BKerestan commented 9 years ago

I'm also looking for something like this. What I have right now is an initialized set of three cards. In each card's listener's onLike and onDislike method, I set it to add a new card. According to my logs, when I swipe the first card, it successfully adds the (in my case with 3 cards initially) fourth card. However, the first card (which I just swiped) flashes back to its initial position and does not respond to any more drags, rather than the second card appearing.

Have any of you guys had different/similar progress?

BKerestan commented 9 years ago

After who knows how long I think I figured this out. Sort of. I don't think this solution is ideal, but it's working for me at the moment.

So the first step is to set up so that the cards are displayed in a First In First Out (FIFO) manner. By default, this library displays Last In First Out, so basically the first cards added to my stack are the last ones shown. This is slightly problematic as it uses an ArrayList to hold the card data. When I use the ArrayList add method, the new data is added to the "top" of the card stack. So to make the adapter endless, I want to display from the "bottom" of the card stack so that I can add data to the top of it without messing up the order in which the cards are presented.

In order to do this, I changed the getCardModel method in the CardStackAdapter class. Anywhere that it had "mData.size() - 1 - position", I replaced that with simply "position". This way, instead of counting down from mData.size-1 to 0, we count from 0 upwards. Running the app now should show the the cards in the order that they are loaded into the cardstack.

In the Activity that adds cards to the adapter, I have a method called AddCard, which creates the card model, assigns its listeners, and then adds it to the arraylist. In the OnCardDismissedListener, I added a call to addCard to both the onLike and onDislike listeners. Running the app, my logcat showed that the card was being added after the swipe, but just like in my comment above, the cards would stop responding to swipes and touches.

I messed around with a few different approaches, but what eventually worked was to create a new method in the cardstackadapter class (I called mine addWithoutNotify()) that is exactly the same as the default add() method, but without the call to notifydatasetchanged(). Then I went back to my Activity's addCard method and added a boolean parameter called notify. When notify is true, then the card is added using the cardstackadapter's default add() method, when it is false, the card is added using the new addWithoutNotify() method. The cards that are added initially are called with notify == true. Then when addCard is called on a swipe, those calls are made with notify == false.

I did this with my CardContainer's MAXVISIBLE set to 3, and starting with about 10 cards. I'm not sure how or if this will work with different settings.

As I said, I'm not sure that this is the best way to implement this functionality, especially as I often have read that calling notifydatasetchanged() is necessary any time data is added to an adapter. However, it's currently working for me.

sirajsumra92 commented 9 years ago

@BKerestan Your solution looks similar to my issue, Can u please take a look at #50

sirajsumra92 commented 9 years ago

@BKerestan @TiagoGouvea I used this library and I have used MaxVisible to 2 with cards added Dynamically, But there are some Bugs in the Card Swipe and Touch Listener.

I am getting wrong card on Swipe and Touch. Can u please guide what are the Changes i have to make to properly use these functionality.?

ahmadmiraj commented 8 years ago

yes i found the solution and also have fixed the problems like wrong car is swiping etc. I can put it on google drive anyone can use that project with fixes. now it works pretty similar like a tinder

avfernandezz commented 8 years ago

@ahmadmiraj could you please send me a link? I'm having trouble with the swiping and would love to see your solution

ahmadmiraj commented 8 years ago

@avfernandezz sure. give me your dropbox or anything i will send you the modified classes just put it there and u will good to go !

ijzepeda commented 8 years ago

@ahmadmiraj Do you mind sharing them publicly?