gpt4 book ai didi

Android view pager 几秒后自动换页

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:46:48 26 4
gpt4 key购买 nike

我已经使用 android view pager 来显示图像和文本,现在我想要的是,如果用户没有更改图像,那么它应该在几秒钟后以及当用户再次开始使用他的手指时自动开始更改图像那么图像应该不会自动改变?

我尝试使用处理程序来创建延迟,但它对我不起作用?

请帮助任何人

public class SimpleImageViewerActivity  extends Activity implements ViewPager.OnPageChangeListener{
private Handler handler;
ViewPager myPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageslideshow);
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.slideShowPager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);

handler = new Handler();
myPager.setOnPageChangeListener(this);
}

private class MyPagerAdapter extends PagerAdapter {

public int getCount() {
return 5;
}

public Object instantiateItem(View collection, int position) {

LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}

View view = inflater.inflate(resId, null);

((ViewPager) collection).addView(view, 0);

return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);

}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);

}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}


}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

updateUI(arg2);
}

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//updateUI(arg0);

}

public void onNothingSelected(AdapterView<?> adapterView) {
final int i =0;
adapterView.postDelayed(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
updateUI(i);
}

}, 5000);
Toast.makeText(this, "In nothing selected method",Toast.LENGTH_SHORT).show();




}

private void updateUI(final int fi) {
/* handler.post(new Runnable() {
public void run() {
gallery.setSelection(i);
viewPager.setCurrentItem(i);

//textView.setText("Photo #" + i);
}
});*/
handler.postDelayed(new Runnable() {
public void run() {
int i=fi;
int j = i;
myPager.setCurrentItem(j++);
System.out.println("Hello");

}
},1000);
}


}

最佳答案

使用 javautil 计时器并设置一个,在计时器结束时你可以使用

class UpdateTimeTask extends TimerTask {
public void run() {
//Code for the viewPager to change view
}
}


// Code to schedule the timer
timer = new Timer();
timer.schedule(new UpdateTimeTask(), 100, 200);

希望这对您有所帮助,请使用 android 文档进行准确的 API 调用

<ViewPager>.setCurrentitem(i++%<MaxViews>)

关于Android view pager 几秒后自动换页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com