延迟执行-最佳做法

时间:2018-06-20 14:51:13

标签: android

在一个特定的片段中,我通过单击按钮调用许多片段事务。
然后在按钮上开始动画。

我想让动画运行2秒钟,然后再进行其他操作。
C#中有一个简单的方法可以做到这一点,而Java中有什么合适的方法呢?

public class Collapsebutton_Fragment extends Fragment {

Collapsebutton_Fragment cpb_fragment;
Animation_Fragment anim_fragment;
FragmentManager fragmentManager;

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

    View rootView = inflater.inflate(R.layout.collapsebutton_fragment, container, false);

    //Look for an animation xml file with a good collapse animation
    final Animation collapse = AnimationUtils.loadAnimation(getActivity(),R.anim.collapse_animation);
    final Button button = (Button)rootView.findViewById(R.id.CollapseButton);
    cpb_fragment = new Collapsebutton_Fragment();
    anim_fragment = new Animation_Fragment();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(collapse);

            //wait for a certain time, then remove the button and start animation
            //remove is not functioning, why not?
            getFragmentManager().beginTransaction()
                    .remove(getFragmentManager().findFragmentById(R.id.CollapseButton))
                    .commit();

            //does not wait until the animation is finished
            getFragmentManager().beginTransaction()
                    .add(R.id.container_mainactivity,anim_fragment)
                    .addToBackStack(null)
                    .commit();
        }
    });

    return rootView;
}
}

2 个答案:

答案 0 :(得分:2)

您可以将片段代码放入处理程序中,并使用处理程序的post延迟方法 在您的onClickMethod中,您可以编写处理程序并在run方法中替换代码正文。

final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
    @Override
    public void run() {
        //Put your Fragment code here
    }
},2000L);

答案 1 :(得分:0)

谢谢@klingklang ..

@Override
        public void onClick(View v) {
            v.startAnimation(collapse);
            collapse.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //remove is not functioning, why not?
                    /*getFragmentManager().beginTransaction()
                    .remove(getFragmentManager().findFragmentById(R.id.CollapseButton))
                    .commit();*/

                    //repeat animation for 5 seconds, is not done yet
                    getFragmentManager().beginTransaction()
                            .add(R.id.container_mainactivity,anim_fragment)
                            .addToBackStack(null)
                            .commit();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    });