mudit
mudit

Reputation: 25534

Swipe to Add view in android

I want to achieve something like the below gif:

enter image description here

What I have done so far:

I am using a swipelayout using I am able to implement the part where if you swipe the row item, it shows you the "Add to Cart" icon but I am not able to implement the animation where it zooms in the cart icon and add it to cart (I am not able to create a hook where I can call the addToCart function).

Below is my code:

from RecyclerView Adapter:

SwipeLayout swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
    swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
                            @Override
                            public void onStartOpen(SwipeLayout layout) {

                            }

                            @Override
                            public void onOpen(SwipeLayout layout) {

                            }

                            @Override
                            public void onStartClose(SwipeLayout layout) {

                            }

                            @Override
                            public void onClose(SwipeLayout layout) {

                            }

                            @Override
                            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
        // i tried leveraging the leftOffset which tells me the position of the //layout on the screen but because it is called multiple times, It was //adding many entries to the cart in just one swipe :(
                            }

                            @Override
                            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

                            }
                        });

I would really appreciate if anyone can suggest a better way to fix this or any open-source library which does such a thing.

Many thanks!

PS: Please add a comment if you need more information from me in order to help me. :)

Upvotes: 1

Views: 415

Answers (1)

Budius
Budius

Reputation: 39836

what I'll write is a best guess based on the information you gave, but it seems that it's all right there:

@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
    float percent = ((float)leftOffset/((float)totalWidth));
    // use `percent` to animate the icon, something like:
    icon.setAlpha(percent);
}

@Override
public void onOpen(SwipeLayout layout) {
    // here is the swipe fully open
    addToCart(item); // create the method
    layout.close(); // close the layout again
}

Upvotes: 1

Related Questions