Performing action after fragment transaction animation is finished

前端 未结 5 605
旧巷少年郎
旧巷少年郎 2020-11-30 01:23

I want to set a buttons visibility after the animation is finished.

That\'s what calls the animation:

android.support.v4.app.FragmentTransaction fAni         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 01:26

    Combining the answers above here is a sample I am using successfully with the support library fragments.

    Simply extend the MenuFragment and set the listener to get a callback of what to execute afterwards.

    public class MenuFragment extends Fragment {
    
    private WeakReference onMenuClosedListener;
    
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Animation anim = null;
        if (enter) {
            anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_slide_in_top);
        } else {
            anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_menu_slide_out_top);
            anim.setAnimationListener(new AnimationListener() {
                @Override public void onAnimationStart(Animation animation) {
                }
                @Override public void onAnimationRepeat(Animation animation) {
                }
                @Override public void onAnimationEnd(Animation animation) {
                    onMenuClosed();
                }
            });
        }
    
        // NOTE: the animation must be added to an animation set in order for the listener
        // to work on the exit animation
        AnimationSet animSet = new AnimationSet(true);
        animSet.addAnimation(anim);
    
        return animSet;
    }
    
    private void onMenuClosed() {
        if (this.onMenuClosedListener != null) {
            OnMenuClosedListener listener = this.onMenuClosedListener.get();
            if (listener != null) {
                listener.onMenuClosed();
            }
        }
    }
    
    public void setOnMenuClosedListener(OnMenuClosedListener listener) {
        this.onMenuClosedListener = new WeakReference(listener);
    }
    
    /**
     * Callback for when the menu is closed.
     */
    public static interface OnMenuClosedListener {
    
        public abstract void onMenuClosed();
    
    }
    

    }

提交回复
热议问题