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)
答案 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)
我想在关闭视图/片段之前执行滑动退出动画。 这些是我执行的步骤:
下面是一些想法的改动版本。
我使用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