动画侦听器不工作按钮单击

时间:2014-04-29 09:03:23

标签: android android-animation

我试图在按钮点击时隐藏/显示TableLayout但是动画监听器在这里不起作用是我正在尝试的代码

Slide_down = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
Slide_up = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_up);

searchArea = (TableLayout) findViewById(R.id.TableLayout1);
        SearchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (check_tableView == 0) {

                    Slide_up.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            searchArea.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            searchArea.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });


                    check_tableView = 1;
                } else {

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

                            searchArea.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            searchArea.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    check_tableView = 0;
                }
            }

        });

2 个答案:

答案 0 :(得分:1)

//刚开始动画

searchArea = (TableLayout) findViewById(R.id.TableLayout1);
        SearchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (check_tableView == 0) {


searchArea.startAnimation(Slide_up);
}
});

答案 1 :(得分:0)

您只需在onClickListener之外设置动画侦听器。每次单击视图时无需设置。还要确保调用searchArea.startAnimation(Slide_up)和searchArea.startAnimation(Slide_down)方法。在onClickListener中调用start动画方法。