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);
}