Animate fragments content before closing it

时间:2016-10-20 13:14:24

标签: android animation fragment

I am displaying a FrameLayout with some components inside a Fragment. In onCreateView(..) I am animating the content of the FrameLayout and everything works fine. Now I want to animate the content before closing the Fragment.

In my current solution I am overriding onBackPressed() in the parent Activity and then I'm calling the method onBackPressed() inside my Fragment and animating the content there. The problem with this solution is, that I want to inflate the Fragment from various activities and then this is not really a nice solution... Does anybody know a better approach?

Thanks for your help!

Note:

I also tried to override onCreateView() and onPause() but the animation is not shown if I start it in those methods

and the following method does not fulfill my requirements either as it animates the whole fragment and I want to animate the content

getActivity().getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up, R.anim.slide_out_down, R.anim.slide_in_down)

3 个答案:

答案 0 :(得分:2)

也许您可以尝试在onBackPressed中处理fragment,如下所示:

    yourRootLayout.setFocusableInTouchMode(true);
    yourRootLayout.requestFocus();
    yourRootLayout.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                animateMyViews();
                return true;
            }
            return false;
        }
    });

答案 1 :(得分:1)

我想在关闭视图/片段之前执行滑动退出动画。 这些是我执行的步骤:

  1. 我创建了一个可运行的任务,可以关闭当前屏幕。
  2. 将该可运行对象传递给动画视图。
  3. 使用了view.postOnAnimationDelayed(runnable,400),以便它可以动画并在400毫秒后执行可运行对象。
  4. 我还确保动画持续时间> = 400,以使过渡平滑。

下面是一些想法的改动版本。

我使用view.postOnAnimationDelayed(runnable,400)来

Dialog dialog = new Dialog(getActivity(), getTheme()) {
            @Override
            public void onBackPressed() {
                ParentFragment.this.onBackPressed();
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        //Referencing this class in runnable
                        getInstance().dismiss();
                    }
                };
             
             //webView is the child view loaded on this fragment
             if(webView != null && webView.webViewClient != null) {
                webView.webViewClient.animateClose(webView, runnable);
             } else {
                super.dismiss();
             }
        };

webViewClient中的animateOnClose函数如下所示:

public void animateClose(final WebView view, Runnable runnable) {
  Animation anim = AnimationUtils.loadAnimation(getMainActivityContext(),
               animationResource);
        view.startAnimation(anim);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.postOnAnimationDelayed(runnable, 400);
            // you can also try view.postOnAnimation(runnable);
        } else {
            runnable.run();
        }
}

答案 2 :(得分:0)

我认为您需要通知Fragment您即将销毁它。

粗略的伪代码可能看起来像

myFragment.aboutToClose();

使用Fragment的{​​{1}}方法。

aboutToClose()

最后在您的来电public void aboutToClose() { // Perform all the animations you want. // Don't forget to add onAnimationEnd() call back. onAnimationEnd() { // Notify Activity that Animations have completed. callback.animationsCompleted(); } }

Activity