【问题标题】:Animate a view in fragment before onDestroy or OnDestroyView在 onDestroy 或 OnDestroyView 之前为片段中的视图设置动画
【发布时间】:2016-05-11 22:37:33
【问题描述】:

我没有找到任何几乎相关的东西。 如何在调用 onDestroyonDestroyView supers 之前为视图设置动画?

我试过了

@Override
public void onDestroyView() {
    Animation fadeOut = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out_fast);
    mBackgroundView.setAnimation(fadeOut);
    super.onDestroyView();
}

这样super.onDestroyView会立即停止动画。

fadeOut 设置一个监听器,然后调用MyFrag.super.onDestroyView() onAnimationEnd() 也不起作用,因为必须在方法本身内部调用super。

我该怎么做?提前致谢!

【问题讨论】:

  • 我认为 onDestroyView() 对于你想要完成的事情来说为时已晚。你试过 Fragment 的 onPause() 方法吗?
  • 是的,谢谢.. 已经试过了,还是没有

标签: android android-fragments android-animation ondestroy


【解决方案1】:

我终于做到了。

工作是通过一个回调接口和Activity中的onBackPressed按钮来完成的。

public interface UiCallback {
    void update();
}

活动中:

UiCallback mUiCallbackInstance = new UiCallback() {
    public void update( /* Remove the fragment from stack here */)
}

@Override
public void onBackPressed() {
    mFragmentInstance.remove(mUiCallbackInstance)
}

在片段中:

public void remove(UiCallback callback) {
    Animation anim = AnimationUtils.loadAnimation(...);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            callback.update();
        }
    })
    // start animation
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多