and*_*per 9 animation android android-fragments
我有一个活动,片段需要在创建时动画,但不是在方向改变时.
片段被动态插入到布局中,因为它是导航抽屉式活动的一部分.
我想避免为配置更改重新创建片段,所以我在片段中使用了setRetainInstance.它可以工作,但由于某些原因,每次旋转设备时动画也会重新启动.
我已将此添加到片段中:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
Run Code Online (Sandbox Code Playgroud)
这对活动:
final FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment= (MyFragment) fragmentManager.findFragmentByTag(MyFragment.TAG);
if (fragment== null) {
fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
fragment= new MyFragment();
fragmentTransaction
.add(R.id.fragmentContainer, fragment, MyFragment.TAG).commit();
}
Run Code Online (Sandbox Code Playgroud)
fragment_container.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
这个解决方案通常起作用,但它会给你试图实现的生命周期带来不好的结果:
MyFragment fragment= (MyFragment) fragmentManager.findFragmentByTag(MyFragment.TAG);
if (MyFragment== null) {
MyFragment= new MyFragment();
fragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
.replace(R.id.fragmentContainer, fragment, MyFragment.TAG).commit();
} else {
//workaround: fragment already exists, so avoid re-animating it by quickly removing and re-adding it:
fragmentManager.beginTransaction().remove(fragment).commit();
final Fragment finalFragment = fragment;
new Handler().post(new Runnable() {
@Override
public void run() {
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment, finalFragment .TAG).commit();
}
});
}
Run Code Online (Sandbox Code Playgroud)
我仍然希望看到可以做什么,因为这可能导致你不想发生的事情(例如onDetach片段).
解决此问题的一种方法是避免通过fragmentManager添加动画,并在片段生命周期内为视图本身执行此操作.这是它的样子:
BaseFragment
@Override
public void onViewCreated(final View rootView, final Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
if (savedInstanceState == null)
rootView.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_from_left));
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (!getActivity().isChangingConfigurations())
getView().startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));
}
Run Code Online (Sandbox Code Playgroud)
onCreateAnimation()如果在旋转后重新创建片段,您如何覆盖该方法并阻止动画发生?
如下所示:如何在屏幕旋转后禁用/避免片段自定义动画
编辑:这是一个示例代码:
BaseFragment.java
...
private boolean mNeedToAvoidAnimation;
@Override
public void onDestroyView() {
super.onDestroyView();
mNeedToAvoidAnimation = true;
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
// This avoids the transaction animation when the orienatation changes
boolean needToAvoidAnimation = mNeedToAvoidAnimation;
mNeedToAvoidAnimation = false;
return needToAvoidAnimation ? new Animation() {
} : super.onCreateAnimation(transit, enter, nextAnim);
}
Run Code Online (Sandbox Code Playgroud)
此片段应该是此活动中所有片段将扩展的基础片段.
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |