我有 4 个以上 fragment (android.support.v4.app.Fragment
)。
Fragment f1, f2, f3, f4;
int F1 = 1; int F2 = 2; int F3 = 3; int F4 = 4;
protected void onCreate(Bundle savedInstanceState)
{
//...
f1 = new Fragment();
f2 = new Fragment();
f3 = new Fragment();
f4 = new Fragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fl_root, f1)
.add(R.id.fl_root, f2)
.add(R.id.fl_root, f3)
.add(R.id.fl_root, f4)
.commit();
setFragment(F1);
}
public void setFragment(int fragment)
{
switch (fragment)
{
case F1:
if (!f1.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f1)
.hide(f2)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F2:
if (!f2.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f2)
.hide(f1)
.hide(f3)
.hide(f4)
.commit();
}
break;
case F3:
if (!f3.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f3)
.hide(f1)
.hide(f2)
.hide(f4)
.commit();
}
break;
case F4:
if (!f1.isVisible()) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.rtol, R.anim.ltor, 0, 0)
.show(f4)
.hide(f1)
.hide(f2)
.hide(f3)
.commit();
}
break;
}
在setFragment
到第一个、第二个等等(1->2->3->4)之后rtol
(从右到左翻译)动画起作用,并且没关系。
但回去后,使用setFragment
(4->3->2->1),仍然有rtol
动画,而不是正确的ltor
(从左到右翻译)。
我做错了什么,解决方案是什么?
最佳答案
实际上我的项目中的单个 Activity 有 20 多个 fragment 。
我解决了这个动画问题 + OutOfMemory
(因为添加和隐藏了 20 多个 fragment )。
一点代码:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (!back)
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
else
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right);
switch (fragment)
{
case F1:
ft.replace(R.id.fl_root, new F1(), String.valueOf(F1));
break;
//...
}
关于java - Android fragment 翻译动画显示、隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502617/