2

The app I'm trying to develop has a ViewFlipper and some buttons bellow corresponding to views, you click on the third button and the ViewFlipper changes views to the 3rd image, the problem is that once I click that button the timer doesn't reset to 0, so for example if I was on the second image for 4 secs and then if I change to the 3rd image, after 1 sec the ViewFlipper will change to the 4th image, I wanted the timer to reset back to 0 so that way it would stay on the view that I changed to for 5 secs.

Here is my code:

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

    }
4

2 回答 2

2

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

       viewFlipper.setDisplayedChild(num);
            viewFlipper.stopFlipping();
            viewFlipper.setFlipInterval(flippertime);
            viewFlipper.startFlipping();
于 2020-03-28T08:14:47.580 回答
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);

    }
于 2020-03-28T07:58:25.633 回答