我如何将ViewFlipper中的计时器重置为0

问题描述 投票:1回答:2

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

这是我的代码:

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

    }
java android viewflipper android-viewflipper
2个回答
0
投票

好吧,我找到了答案,我需要做的是使用一个处理程序,首先的问题是该处理程序将覆盖其他先前的处理程序,当我更改为一个视图然后又更改为另一个视图时,两个处理程序仍在等待,所以我需要添加的只是这一行handler.removeCallbacks(runnable);

这是我当前的完整代码:

@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) {
                        try{
                            handler.removeCallbacks(runnable);
                        } catch(Exception e){}
                        viewFlipper.setDisplayedChild(num);
                        viewFlipper.stopFlipping();
                        handler = new Handler();
                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                viewFlipper.showNext();
                                viewFlipper.startFlipping();
                            }
                        };   handler.postDelayed(runnable, 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);

    }

0
投票

将此添加到您的image2 click监听器中:

       viewFlipper.setDisplayedChild(num);
            viewFlipper.stopFlipping();
            viewFlipper.setFlipInterval(flippertime);
            viewFlipper.startFlipping();
© www.soinside.com 2019 - 2024. All rights reserved.