问题描述
在活动中,只需按一下按钮,就可以使用自定义动画将当前片段替换为新片段,如本例所示.
In my activity, with the touch of a button, I replace the current fragment with a new fragment using a custom animation, like in this example.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_anomalie:
Fragment contentFragment = getFragmentManager().findFragmentById(R.id.content);
if(contentFragment instanceof AnomalieListFragment)
{
getFragmentManager().popBackStack();
return true;
}
else
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
anomalieFragment = new AnomalieListFragment();
ft.replace(R.id.content, anomalieFragment);
ft.addToBackStack(null);
ft.commit();
}
...
但是,弹出堆栈不会显示任何动画.有没有办法像在FragmentTransaction中那样通过 setCustomAnimations 方法?
However, popping back the stack doesn't show any animation.Is there a way to specify a custom animation like we do in a FragmentTransaction with the setCustomAnimations method?
推荐答案
进一步阅读文档后,我发现使用 setCustomAnimation
的此签名允许在按下后退按钮或调用 getFragmentManager().popBackStack();
After further reading of the documentation, I found that using this signature of setCustomAnimation
allowed the animation to be played when pressing the back button or calling getFragmentManager().popBackStack();
我这样修改了我的代码
...
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out);
anomalieFragment = new AnomalieListFragment();
ft.replace(R.id.content, anomalieFragment);
ft.addToBackStack(null);
ft.commit();
...
这篇关于在FragmentManager上调用popBackStack时的customAnimation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!