我使用计时器自动滑动viewpager。但到达最后一页后,计时器停止。如何让它从第一页再次继续?
final Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
pager.setCurrentItem(page++);
}
};
int delay = 1000; // delay for 1 sec.
int period = 4000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
答案 0 :(得分:3)
最新答案 2021 设置 viewPager 适配器后
private Runnable runnable = null;
public void function DisplaySlider(){
sliderItemAdapter = new SliderItemAdapter(getActivity(), mSliderList);//push the data to the adapter
mViewPager.setAdapter(sliderItemAdapter); //set the adapter to the view pager
startAutoSlider(sliderItem.getCount());
}
private void startAutoSlider(final int count) {
runnable = new Runnable() {
@Override
public void run() {
int pos = mViewPager.getCurrentItem();
pos = pos + 1;
if (pos >= count) pos = 0;
mViewPager.setCurrentItem(pos);
handler.postDelayed(runnable, 3000);
}
};
handler.postDelayed(runnable, 3000);
}
最后不要忘记从回调中释放内存
@Override
public void onDestroy() {
if (runnable != null) handler.removeCallbacks(runnable);
super.onDestroy();
}
答案 1 :(得分:1)
final Runnable mUpdateResults = new Runnable() {
public void run() {
int numPages = pager.getAdapter().getCount();
page = (page + 1) % numPages;
pager.setCurrentItem(page);
}
};
当超过寻呼机限制
时,将当前项设置为0答案 2 :(得分:-1)
下面的答案会有所帮助;
$data = array();
array_push($data,"10:00PM");
array_push($data,"12:00PM");
echo json_encode($data);