简体   繁体   English

如何在android中替换片段时检查完成setCustomAnimations

[英]How to check finish setCustomAnimations when replace fragment in android

In my activity or fragment I used method: 在我的活动或片段中我使用了方法:

  public void replaceFragment(Fragment fragment, boolean addToBackStack) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.setCustomAnimations(R.anim.slide_in_right, 0); if (addToBackStack) { transaction.addToBackStack(null); } transaction.replace(R.id.mFrameContainer, fragment); transaction.commitAllowingStateLoss(); } 

when replace fragment. 当替换片段时。 I run but in some screen have load data from sever, it's lag when show fragment so I want when animation finish I will call method load data. 我运行,但在某些屏幕上有来自服务器的加载数据,它显示片段时滞后所以我想在动画完成时我会调用方法加载数据。 So how I can check finish animation replace fragment. 那我怎么能检查完成动画替换片段。 I search and see in stack over but I still can't do it. 我在堆栈中搜索并查看,但我仍然无法做到。 If you have answer for my question, please share to me! 如果您有我的问题的答案,请分享给我! Thank you very much! 非常感谢你!

Instead of importing import android.app.Fragment; 而不是导入import android.app.Fragment; , use import android.support.v4.app.Fragment; ,使用import android.support.v4.app.Fragment; . Change getFragmentManager in your replaceFragment method with getSupportFragmentManager . 使用getSupportFragmentManagerreplaceFragment方法中更改getFragmentManager Then, in your fragment class, add the following: 然后,在您的片段类中,添加以下内容:

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

    Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);

    anim.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO: This is where you will put your code for when the animation is finished
        }
    });

    return anim;
}

Put the code you want to execute after in onAnimationEnd . 将您想要执行的代码放在onAnimationEnd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM