Skip to content

Is there a way to include a Multi-transition within ViewPager2?

Am using an array list that holds 5 images which change after an interval of 5seconds on the viewpager2 using handler and runnable,the images change perfectly well from the 1st image to the 5th image and restarts the process again using slide transition, the problem i have is changing the animation as it restarts running through the item images it looks terrible because it has to slide back to the first time as it transits more faster through the items backwards , i want to create it in a disappear(last item) and reappear (1st item) fast transition just like when popup ads change images and restart the slide transition again to the last item in a fragment, below is the piece of code.

 viewpager3.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            sliderHandler.removeCallbacks(sliderRunnable);

                sliderHandler.postDelayed(sliderRunnable, 5000);





        }
    });

      sliderRunnable =new Runnable() {

        @Override
        public void run() {
            if(viewpager3.getCurrentItem()<image2List.size()-1)
            { viewpager3.setCurrentItem(viewpager3.getCurrentItem()+1);
            }else {
                viewpager3.setPageTransformer();

                viewpager3.setCurrentItem(0);
            }


        }
    };

Answer

Not sure why you are using Viewpager2 for this and have to automate the changing of views yourself and you would really also need to implement an infinite View pager to get a more seamless transition (though at a small risk if running out of pages)

You could try setting viewpager3.setCurrentItem(0,false); to jump to the start without any animation.

But much better would be to use the more appropriate AdapterViewFlipper class.

This has automatic changing of the item based on a timer built in to it (no runnables needed).

It recyclers and is dynamic like viewpager2, the only downside is that page(view) is not as nicely encapsulated in a Fragment but as you are just showing images then that should not be a problem.

Example of how to use https://abhiandroid.com/ui/adapterviewflipper

You can add in and out animations to slide in and out to get a similar look to the Viewpager2 animations

If you look at another examples of the similar class for more static content it shows the slide in/out animations https://abhiandroid.com/ui/viewflipper