- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在打开和关闭时为 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);
玩这些直到你得到你想要的行为。
关于android - fragment popbackstack 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492106/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!