Skip to content

myinnos/Swiggy-Scroll-Animation

Repository files navigation

Swiggy Offer Scroll Animation

Swiggy Offer Scroll Animation - Example

Download Demo APK from HERE

Use the following code for the awesome animation:

To build this animation used RecyclerView as it has a OnScrollListener. Ref.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                adpater.recyclerViewScrolled(recyclerView, dy);
            }
        });

In the callback onScrolled() we notify the adapter using a custom method recyclerViewScrolled()and pass the reference of the view and the amount of vertical scroll.

After the adapter is notified, it is his responsibility to get the view holders for all the items and rotate the image view. Let’s see how can this be done. Ref.

void recyclerViewScrolled(RecyclerView recyclerView, int scrollAmount) {

        RecyclerAdapter.ViewHolder holder = null;

        int starPos = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        int lastPos = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();

        if (starPos <= lastPos) {
            while (true) {
                holder = (RecyclerAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(starPos);
                if (holder != null) {
                    holder.imOffer.setRotation(holder.imOffer.getRotation() + (float) scrollAmount);
                }
                if (starPos == lastPos) {
                    break;
                }
                ++starPos;
            }
        }
    }

That's it your awesome animation ready

Any Queries? or Feedback, please let me know by opening a new issue!

Contact

Prabhakar Thota

If you appreciate my work, consider buying me a cup of ☕ to keep me recharged 🤘 by PayPal

License

Copyright 2019 MyInnos

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.