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

Swiping up and down #64

Closed vanshg closed 8 years ago

vanshg commented 9 years ago

In addition to the swiping left and right to "like" or "dislike" something, is it possible to also add the ability to swipe up and down for other actions?

Dvik commented 8 years ago

@vanshg did you find any way to swipe up/down for like/dislike?

vanshg commented 8 years ago

I did.

Change the if statements in CardContainer to

if (cardModel.getOnCardDismissedListener() != null) {
                    if (targetX > 0 && (targetY < 1500 && targetY > -1500)) {
                        cardModel.getOnCardDismissedListener().onLike(cardModel);
                    }
                    else if (targetX < 0 && (targetY < 1500 && targetY > -1500)) {
                        cardModel.getOnCardDismissedListener().onDislike(cardModel);
                    }
                    else if (targetY < 0 && (targetX < 1000 && targetX > -1000)) {
                        cardModel.getOnCardDismissedListener().onUp(cardModel);
                    }
                    else if (targetY > 0 && (targetX < 700 && targetX > -700)) {
                        cardModel.getOnCardDismissedListener().onDown(cardModel);
                    }
                    else {
                        topCard.animate()
                                .setDuration(duration)
                                .setInterpolator(new DecelerateInterpolator())
                                .translationX(0)
                                .translationY(0)
                                .rotation(0);
                        return true;
                    }

You must also change the OnCardDismissedListener in CardModel.java to be defined as follows

public interface OnCardDismissedListener
    {
        void onLike(CardModel cardModel);

        void onDislike(CardModel cardModel);

        void onUp(CardModel cardModel);

        void onDown(CardModel cardModel);
    }

When you set your OnCardDismissedListener, simply define onUp and onDown as well as onLike and onDislike like so:

card.setOnCardDismissedListener(new CardModel.OnCardDismissedListener()
            {
                @Override
                public void onLike(CardModel cardModel)
                {

                }

                @Override
                public void onDislike(CardModel cardModel)
                {

                }

                @Override
                public void onUp(CardModel cardModel)
                {

                }

                @Override
                public void onDown(CardModel cardModel)
                {

            });
Dvik commented 8 years ago

i'll try the code. i need the up/down as well as left/right. for that i should define all four in the if statement above. Right?

vanshg commented 8 years ago

The code i posted is for up/down as well as left/right

Dvik commented 8 years ago

yeah, thanks.

Dvik commented 8 years ago

i tried it now. made the changes specified above but up/down are still not working.

vanshg commented 8 years ago

What error do you get?

Dvik commented 8 years ago

No error. I am toasting a msg on up,left and right. The card doesn't get dismissed when I drag it to top.

Dvik commented 8 years ago

vansh, can you help me in this regard?

vanshg commented 8 years ago

Try logging the X and Y values right before the if statement and see what they are when you try to swipe the cards up and down and then adjust the values in the if statement accordingly. Other than trying that, I'm not sure