2
\$\begingroup\$

I'm changing the Viewpager items programmatically using mViewPager.setCurrentItem(position); with a regular interval of time recursively. Please tell me if this is the best way to achieve this or if there are any problems with my code.

    ViewPager mViewPager ;
    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    Timer timer = new Timer();
    timer.schedule(new UpdateTimeTask(backgroundimages.length), 1, 5000);

    private class UpdateTimeTask extends TimerTask {
        int mLength = 0;

        public UpdateTimeTask(final int length) {
            this.mLength = length;
        }

        public void run() {
            mViewPager.post(new Runnable() {
                @Override
                public void run() {
                    if (mViewPager.getCurrentItem()+1 < mLength) {
                        Updateviewpager(mViewPager.getCurrentItem() + 1);
                    } else {
                        Updateviewpager(0);
                    }
                }
            });
        }
    }

    public void Updateviewpager(int position) {
        mViewPager.setCurrentItem(position);
    }
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Timers and TimerTasks appear to be discouraged for Android, especially when interacting with the UI. Each Timer gets a thread on which to do its work, which makes UI updates problematic, and which also presumably causes unnecessary overhead for small tasks.

This could be updated to use the postDelayed() or postAtTime() method of Handler, which takes advantage of the existing message queue. Note: this code is not tested.

ViewPager mViewPager ;
mViewPager = (ViewPager) findViewById(R.id.viewpager);

private class UpdateTimeTask implements Runnable {
    private static final int TASK_TIME_MS = 5000;

    private int mLength = 0;
    private Handler mHandler;

    public UpdateTimeTask(final int length) {
        this.mLength = length;
        // Get the main thread's Handler. If the task is
        // always on the main thread, we can just do
        // mHandler = new Handler();
        // Otherwise, do:
        mHandler = new Handler(Looper.getMainLooper());

        // Get things started.
        // If this is intended to be done on the UI thread,
        // just call run() directly.
        // run();
        // If this is intended to be done on any thread, use
        // the Handler to execute run()
        mHandler.post(this);
    }

    public void run() {
        if (mViewPager.getCurrentItem()+1 < mLength) {
            Updateviewpager(mViewPager.getCurrentItem() + 1);
        } else {
            Updateviewpager(0);
        }

        // Schedule to repeat
        mHandler.postDelayed(this, TASK_TIME_MS);
    }
}

public void Updateviewpager(int position) {
    mViewPager.setCurrentItem(position);
}

References:
Android Timer Documentation
Updating the UI from a Timer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.