gpt4 book ai didi

java - 如何将 ViewFlipper 中的计时器重置回 0

转载 作者:行者123 更新时间:2023-12-04 10:20:15 30 4
gpt4 key购买 nike

我正在尝试开发的应用程序有一个 ViewFlipper 和一些对应于 View 的按钮,您单击第三个按钮,ViewFlipper 将 View 更改为第三个图像,问题是一旦我单击该按钮,计时器就不会重置到 0,例如,如果我在第二张图像上停留 4 秒,然后如果我更改为第三张图像,1 秒后 ViewFlipper 将更改为第四张图像,我希望计时器重置回 0,这样它会停留在我更改为 5 秒的 View 上。

这是我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);

viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}

for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}

viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}

public void flipperimages(View image){

viewFlipper.addView(image);

viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);

viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);

}

最佳答案

将此添加到您的 image2 单击监听器:

       viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
viewFlipper.setFlipInterval(flippertime);
viewFlipper.startFlipping();

关于java - 如何将 ViewFlipper 中的计时器重置回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60898535/

30 4 0