Swipeable-Cards icon indicating copy to clipboard operation
Swipeable-Cards copied to clipboard

Programmatically swipe left/right

Open matteinn opened this issue 9 years ago • 9 comments

Hi there! First of all I need to thank you for this cool library.

I need to add like/dislike buttons just like the Tinder app and I'm wondering if there is an easy way to trigger the swipe animation programmatically by tapping the two buttons.

matteinn avatar Sep 17 '14 17:09 matteinn

Hi guys, for those interested in this feature I've added two methods to CardContainer.java

public void dislike(){

        final View topCard = mTopCard;

        mTopCard = getChildAt(getChildCount() - 2);
        CardModel cardModel = (CardModel)getAdapter().getItem(0);

        if(mTopCard != null)
            mTopCard.setLayerType(LAYER_TYPE_HARDWARE, null);

        topCard.animate()
            .setDuration(500)
            .alpha(.75f)
            .setInterpolator(new LinearInterpolator())
            .x(-topCard.getWidth())
            .y(topCard.getY())
            .rotation(-45)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    removeViewInLayout(topCard);
                    ensureFull();
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    onAnimationEnd(animation);
                }
            });

        if(cardModel.getOnCardDimissedListener() != null){
            cardModel.getOnCardDimissedListener().onDislike();
        }
    }
public void like(){
        final View topCard = mTopCard;

        mTopCard = getChildAt(getChildCount() - 2);
        CardModel cardModel = (CardModel)getAdapter().getItem(0);

        if(mTopCard != null)
            mTopCard.setLayerType(LAYER_TYPE_HARDWARE, null);

        topCard.animate()
            .setDuration(500)
            .alpha(.75f)
            .setInterpolator(new LinearInterpolator())
            .x(topCard.getWidth())
            .y(topCard.getY())
            .rotation(45)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    removeViewInLayout(topCard);
                    ensureFull();
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    onAnimationEnd(animation);
                }
            });

        if(cardModel.getOnCardDimissedListener() != null){
            cardModel.getOnCardDimissedListener().onLike();
        }
    }

Of course you can adjust the animation's parameters to fit your needs. I have made several adjustment to the library based on my project's needs (including fading green and red stamps over the photos), so I'm not able to submit a pull request now: I'll try to do it as soon as possible when the project will be completed.

Bye

matteinn avatar Sep 17 '14 17:09 matteinn

Thanks alot matteinn... Its very cool. Thanks.

katikarganesh avatar Sep 18 '14 06:09 katikarganesh

Thanks matteinn. You're so sweet!!! I just need the methods at the moment!.

shumin0809 avatar Sep 18 '14 19:09 shumin0809

I added below lines in like and dislike methods to have like and dislike drawables:

((ImageView) getChildAt(getChildCount() - 1).findViewById(
            R.id.interested_image)).setVisibility(View.VISIBLE);

((ImageView) getChildAt(getChildCount() - 1).findViewById(
            R.id.not_interested_image)).setVisibility(View.VISIBLE);

I just added two imageviews(not_interested_image and interested_image) one on another overlaying on card in xml and made their Visibility "gone" by default.

seshuvinay avatar Sep 26 '14 05:09 seshuvinay

@matteinn where From i call like() and dislike(). And also touch is not sensitive, If having any idea?

SakthivelA avatar Nov 29 '14 11:11 SakthivelA

will you ever submit your pull request @matteinn?

fadelakin avatar Mar 10 '15 11:03 fadelakin

@fadelakin @matteinn Yeah great work. Would love to see a pull.

aeroechelon avatar May 20 '15 18:05 aeroechelon

Thanks Matteinn.

I was in huge trouble.

After searching a lot, i found your solution and it was best.

Thanks once again.

monishagarwal215 avatar Jul 17 '15 12:07 monishagarwal215

Hi Matteinn,

When i click the button very fast before animation end it gives array index out of bounds exception.

I tried a a lot to resolve this issue but unable to resolve.

Please help me out.

monishagarwal215 avatar Aug 18 '15 10:08 monishagarwal215