removeAllViews Android上的动画

时间:2014-03-24 16:55:30

标签: android android-layout android-fragments android-animation android-view

我有一个课程,我在按钮点击中包含另一个布局。这包括布局有一些按钮和一个代码,点击这些按钮时执行。我使用了一个计数器,表示点击按钮的次数。首次单击按钮包括布局,第二次单击删除视图等。这是代码

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++;
        }





        }       
}

最后的代码

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

删除视图但无法处理动画。我尝试过使用removeView,但在这种情况下,if语句中的buttonclicks第二次无法执行。我在这里错过了什么?我怎样才能实现它?

1 个答案:

答案 0 :(得分:1)

答案很简单。动画结束后,您必须删除视图。这可以非常简单地实现,首先你必须为动画设置一个动画监听器,并在动画完成时调用的onAnimationEnd回调中删除视图。

修改

替换它:

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

如果有任何进一步的问题,请告诉我。