gpt4 book ai didi

android - fragment popbackstack 动画不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:54 27 4
gpt4 key购买 nike

我想在打开和关闭时为 fragment 设置动画。我有淡入和淡出自定义动画 XML 文件。

我在我的支持 FragmentTransaction 上使用 setCustomAnimations,但它所做的只是在我执行 addToBackStack 时设置动画,当我执行 popBackStack 时,它只是消失而没有动画。

这是我的代码 fragment :

private void fragmentAppear(){
fragment = new LoginFragment();
fragmentManager = LoginActivity.this.getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//my XML anim files
fragmentTransaction.setCustomAnimations(R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
fragmentTransaction.replace(R.id.login_fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

private void fragmentDisappear(){
getSupportFragmentManager().popBackStack();
}

在 setCustomAnimations 部分,我使用了 4 个参数,到目前为止,当我调用 fragmentAppear 时,它仅在幻灯片进入之前显示淡出动画,但在调用 fragmentDisappear 时从不。我已经尝试过以许多不同的方式对参数进行排序,我也尝试过使用 setCustomAnimations 的两个参数版本,它所做的只是在 fragment 出现时设置动画。

我正在为我的 fragment 使用 android.support.v4.app 库。

编辑:此外,在没有调用 fragmentDisappear 的情况下按下后退按钮时,动画也不会显示。

过去的代码在 Activity 中,我试图从 fragment 中执行 popBackStack,但它也不起作用。这是关闭我的 fragment 的正确方法吗?

编辑:我将包含 XML 动画:

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="75%p"
android:toYDelta="0%p"
android:fillAfter="true"
android:duration="400" />
</set>

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%p"
android:toYDelta="75%p"
android:fillAfter="true"
android:duration="400" />
</set>

最佳答案

如果您查看代码,您正在用一个新 fragment 替换该 fragment ,但实际上您将添加到返回堆栈设置为 null。为每个 fragment 提供一个标签是一种很好的做法,甚至可以很容易地通过标签找到该 fragment 。将标签添加到您的 fragment ,如下所示。如果它仍然不起作用,那么问题将出在您的动画 xml 文件中。

private void fragmentAppear(){
fragment = new LoginFragment();
fragmentManager = LoginActivity.this.getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//my XML anim files
fragmentTransaction.setCustomAnimations(
R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
fragmentTransaction.replace(
R.id.login_fragment, fragment, "loginFragment");
fragmentTransaction.addToBackStack("loginFragment");
fragmentTransaction.commit();
}

从 Fragment Transaction 文档中,我看到了这个函数,您必须在其中指定适当的动画。

/**
* Set specific animation resources to run for the fragments that are
* entering and exiting in this transaction. The
* <code>popEnter</code>
* and <code>popExit</code> animations will be played for enter/exit
* operations specifically when popping the back stack.
*/
public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
@AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
  1. enter => fragment 进入动画
  2. exit => fragment 退出时的动画。
  3. popEnter => fragment 从返回堆栈进入时的动画。
  4. popExit => 从返回堆栈弹出 fragment 退出时的动画。

玩这些直到你得到你想要的行为。

关于android - fragment popbackstack 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com