我用viewpager创建了一个幻灯片,并计划了4秒的延迟和执行下一张幻灯片。我想手动向左或向右滑动时,它会暂停并等待4秒,然后转到下一张幻灯片,但手动滑动被计时器取消,并在4秒前自动转到正在进行的幻灯片。我搜索了stackoverflow和internet,但没有找到任何解决方案。这是我的密码:
活动\u main.xml:
<RelativeLayout ...>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="220dp"
android:id="@+id/viewPager_id"
android:layout_below="@id/toolbar_id" />
.
.
.
</RelativeLayout>
在mainactivity.java中:
ViewPager viewPager;
SlideShowAdapter adapter;
Handler handler;
Runnable runnable;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager_id);
adapter = new SlideShowAdapter(this);
viewPager.setAdapter(adapter);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
int i = viewPager.getCurrentItem();
if (i==adapter.images.length-1){
i=0;
viewPager.setCurrentItem(i,true);
}else {
i++;
viewPager.setCurrentItem(i,true);
}
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(runnable);
}
},4000,4000);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!