动画上removeAllViews的Andr​​oid [英] Animation on removeAllViews Android

查看:440
本文介绍了动画上removeAllViews的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,我有一个按钮点击另一个布局。这包括布局有一些按钮和一个code,其执行上点击这些按钮。我使用了一个计数器,指示次点击按钮的数量。第一次点击按钮,包括布局,第二次点击删除意见等。这里的code

I have a class where I include another layout on a button click. This included layout has some buttons and a code which executes on clicking these buttons. I have used a counter which indicates the number of times the button is clicked. First time clicking on the button includes the layout and the second time clicking removes the views and so on. Here's the code

public class Home extends Fragment implements OnClickListener {

    int c = 0;
    Button bmain, bnew, bolder;
    RelativeLayout r1;
    View rootView;
    Animation slidedown, slideup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home, container, false);
         bmain = (Button) rootView.findViewById(R.id.btn2);
         bmain.setOnClickListener(this);


        return rootView;
    }

    @Override
    public void onClick(View arg0) {

        ViewGroup con = null;
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);


        //Loading animation
        slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
        slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);

        //The counter indicates the number of clicks.  
        //Needs to be replaced for a better solution.
        //If it's even add view
        if(c%2==0)
        {


            //Adding layout here
            flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));

            //Starting Animation
            flContainer.startAnimation(slidedown);



            //After adding layout we can find the Id of the included layout and proceed from there 
            bnew = (Button) rootView.findViewById(R.id.btntest);
            bnew.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View arg0) {            
                    Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
                }

            });

            bolder = (Button) rootView.findViewById(R.id.btntest1);
            bolder.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View arg0) {
                    Intent form = new Intent(getActivity(),FeedbackForm.class);
                    startActivity(form);

                }

            });

            c++;
        } //If ends here

        //If it's odd remove view
        else
        {


                flContainer.removeAllViews();
                flContainer.startAnimation(slideup);

                //flContainer.removeView(flContainer);
                //flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));

                c++;
        }





        }       
}

在code末

                            flContainer.removeAllViews();
                flContainer.startAnimation(slideup);

删除的看法,但无法处理动画。我一直在使用removeView尝试,但在这种情况下if语句的buttonclicks不能执行第二次。我缺少的是在这里吗?我怎样才能实现呢?

removes the view but fails to process the animation. I have tried using removeView but in that case the buttonclicks in the if statement fail to execute the second time. What am I missing here? How can I achieve it?

推荐答案

答案是pretty简单。你有动画完成后,除去图。这样就可以实现pretty简单,首先你必须设置一个动画监听器为您的动画,并在 onAnimationEnd 回调 - 当动画完成被称为 - 你删除意见。

The answer is pretty simple. You have to remove the view after the animation is finished. This can be achieved pretty simple, first you have to set an animation listener for your animation and in the onAnimationEnd callback - which is called when the animation is finished - you remove the views.

编辑:

替换此:

flContainer.removeAllViews();
flContainer.startAnimation(slideup);

这一点:

slideup.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        flContainer.removeAllViews();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
flContainer.startAnimation(slideup);

如果有任何进一步的问题,让我知道。

If there are any further problems let me know.

这篇关于动画上removeAllViews的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆