0

I have created an Image carousel for my app. Actually, for try, it includes 5 banners.

I would insert timer to switch the banner automatically but I don't find the correct way.

This is my MainActivity file with the slide code:

viewPager = findViewById(R.id.viewPagerId);
    mList = new ArrayList<>();
    mList.add(new ModalClass(R.drawable.banner_eeee , "Title"));
    mList.add(new ModalClass(R.drawable.banner_gggg , "Title"));
    mList.add(new ModalClass(R.drawable.banner_ffff , "Title"));
    mList.add(new ModalClass(R.drawable.banner_gggg , "Title"));
    mList.add(new ModalClass(R.drawable.banner_eeee , "Title"));

    SlideHomepageAdapter slideHomepageAdapter = new SlideHomepageAdapter(this, mList);
    viewPager.setAdapter(slideHomepageAdapter);

Could you help me?

Thanks for your answer.

MrPlunk
  • 29
  • 1
  • 9

2 Answers2

0

You can use a ScheduledExecutorService for this:

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(updateToNextImage, 0, 3000, TimeUnit.MILLISECONDS);

The updateToNextImage Runnable should handle the switching of images. Something like this:

Runnable updateToNextImage = new Runnable() {
    @Override
    public void run() {
        runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    // Modify ui on the main thread
                    viewPager.setCurrentItem(currentImageIndex++ % imageCount, true);
                }
            }
        );
    }
};

Don't forget to close it when you're done with it:

executor.shutdown();
Bram Stoker
  • 1,202
  • 11
  • 14
  • You can also use a 'Handler' like this: https://stackoverflow.com/questions/6242268/repeat-a-task-with-a-time-delay/6242292#6242292 – Bram Stoker Jul 08 '20 at 13:14
  • The problem is that I have some difficult to understand how to implement it with my code. – MrPlunk Jul 08 '20 at 23:35
0

Ok, then the code should to be:

    viewPager = findViewById(R.id.viewPagerId);
    mList = new ArrayList<>();
    mList.add(new ModalClass(R.drawable.banner_eeee , "Title"));
    mList.add(new ModalClass(R.drawable.banner_gggg , "Title"));
    mList.add(new ModalClass(R.drawable.banner_ffff , "Title"));
    mList.add(new ModalClass(R.drawable.banner_gggg , "Title"));
    mList.add(new ModalClass(R.drawable.banner_eeee , "Title"));

    SlideHomepageAdapter slideHomepageAdapter = new SlideHomepageAdapter(this, mList);
    viewPager.setAdapter(slideHomepageAdapter);

    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(updateToNextImage, 0, 3000, TimeUnit.MILLISECONDS);

    Runnable updateToNextImage = new Runnable() {
    @Override
    public void run() {
        runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    // Modify ui on the main thread
                    viewPager.setCurrentItem(currentImageIndex++ % imageCount, true);
                }
            }
        );
    }
};

executor.shutdown();

Sorry but I am new for that.

MrPlunk
  • 29
  • 1
  • 9
  • No, you start/stop it in your lifecycle methods. Create the 'ScheduledExecutorService' as a member variable in your 'Activity' or 'Fragment'. Run the task in 'onStart()' ('scheduleAtFixedRate()' returns a 'Future', you want to store that as a member variable too). Stop the task again in 'onStop()' and call 'shutdown()' in 'onDestroy()' – Bram Stoker Jul 09 '20 at 06:23
  • Take a look at: https://developer.android.com/guide/components/activities/activity-lifecycle – Bram Stoker Jul 09 '20 at 06:24