Disable clicks when fragment adding animation playing

☆樱花仙子☆ 提交于 2021-02-07 18:15:25

问题


I have a method, which perform a fragment adding animation and a new fragment fills all screen:

public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack){
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_right);
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.add(R.id.lay_fragments, fragment);
    commitTransaction(transaction);
}

But when animation playing user still allowed to click on buttons on the view below and make another ui actions, which could cause to unpredictable results. Is there an any way to block all user actions in app, until the end of animation?


回答1:


A modification of Beloo's answer for non-support Fragments keeps the code encapsulated a bit better. Just use the start and stop of the listener to set and clear the flag...

@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {


        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

        });
    }
    return animator;
}



回答2:


As @yedidyak suggest i added listener to fragment animation as follows: In base parent fragment:

 @Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    Animation animation;
    if (nextAnim == 0 ){
        animation = super.onCreateAnimation(transit, enter, nextAnim);
    } else {
        animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {}
            public void onAnimationRepeat(Animation animation) {}

            public void onAnimationEnd(Animation animation) {
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
                // additional functionality
            }
        });
    }
    return animation;
}

In activity add fragment method i added code to block user touch events:

public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack, @MagicConstant FragmentAnimation animation){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    FragmentTransaction transaction = ...
}

FLAG_NOT_TOUCHABLE was added in fragment transaction part of code because user can dispatch some events between it and onAnimationStart call.

If somebody knows a simplier way, it will be appreciate.



来源:https://stackoverflow.com/questions/26971825/disable-clicks-when-fragment-adding-animation-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!