FragmentTransaction上的CustomAnimations无法用于旧片段 [英] CustomAnimations on FragmentTransaction not working for the old fragment

查看:184
本文介绍了FragmentTransaction上的CustomAnimations无法用于旧片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的FragmentActivity开头显示一个Fragment,当我按下Button时显示第二个.到这里为止,这很容易.但是我在动画方面遇到了一个问题.
我添加第二个片段:

My FragmentActivity displays one Fragment at the beginning and a second when I press a Button. Until here, it's easy. But I got one problem with the animations.
I add the second fragment with:

mTChild.add(R.id.container, mFragChild).addToBackStack(null).commit();  

为了使其更加活跃",我在添加Fragment之前声明了CustomAnimations,它看起来像这样:

And to make it more "alive", I declare a CustomAnimations before adding the Fragment, it looks like this:

mTChild.setCustomAnimations(R.anim.slide_in, R.anim.alpha_out, R.anim.alpha_in, R.anim.slide_out);  
mTChild.add(R.id.container, mFragChild).addToBackStack(null).commit();  

我使用了此方法setCustomAnimations(int,int,int,int) (参数:enter,exit,popBack输入,popBack退出),如您在

I used this method setCustomAnimations(int,int,int,int) (parameters: enter, exit, popBack enter, popBack exit) as you can see on setCustomAnimations Documentation. Just to understand my (basic) animations xml:

  1. slide_in>新片段从右到左滑动.
  2. slide_out>返回堆栈=片段幻灯片从左到右.
  3. alpha_out>旧片段以alpha 100-> 0消失.
  4. alpha_in>堆栈=旧片段以alpha 0-> 100出现.

在这一步,我得到了从右侧向内滑动/滑出新片段的信息,该片段发生在旧片段上方.但是对于通常滞后并随着alpha消失/出现的旧片段的效果不会发生.

At this step, I got what I want a slide in/slide out from right for the new fragment which takes place above the old fragment. But the effect for the old fragment which normally stays behind and disappears/appears with the alpha not happens.

我做错了什么?因为,CustomAnimations发生了,但是旧片段(没有被删除的片段)没有发生.
感谢您的回答.

What I am doing wrong? Because, the CustomAnimations happens but not for the old fragment (which it not removed).
Thanks for your answers.

推荐答案

我感到羞耻,我犯了一个愚蠢的错误!

I feel shame, I did a stupid mistake!

很明显,我不能像以前那样这样做,因为旧片段留在后面.因此无法制作 out 动画.

当我问自己的问题时,我回答了我自己的问题:旧碎片(未被清除)" ...我的天!因此,代替:

I answered at my own question when I asked it: "the old fragment (which it not removed)"... my god! So instead of:

mTChild.add(R.id.container, mFragChild).addToBackStack(null).commit();  

我应该这样做:

// REPLACE (remove+add) and not ADD only !
mTChild.replace(R.id.container, mFragChild).addToBackStack(null).commit();  

如果我想要 out 动画.

我找到了一种优雅的方法!实际上,目标是使第一个Fragment可见,并与新的Fragment(a ListView)重叠,并使较旧的10%(用于阴影/效果欠佳).在执行此操作的方式上我错了.

I found an elegant way! Indeed, the goal was to keep the first Fragment visible, overlapping with a new Fragment (a ListView) and make the older less visible to 10% (for giving a shadow/under effect). I was wrong on the way to do this.

SlidingMenu和此答案的启发

Inspired by SlidingMenu and this answer Generate an overlay slide in and move existing Fragment to left, I created a FrameLayout to add() (this time no doubt) the new Fragment which have a View to the left with a gradient background to make the shadow effect.
With this:

mTransaction.add(R.id.container, mChildA, null).hide(mChildA)
            .add(R.id.container2, mFrag, null).commit();  

然后,当我按Button时,我将执行以下操作:

And after, when I press my Button, I do as below:

mTChild.setCustomAnimations(R.anim.slide_in, 0, 0, R.anim.slide_out);  
mTChild.show(mChildA).addToBackStack(null).commit();

实际上,我具有幻灯片,并具有BackStack的反效果,并且我在内容上覆盖了左侧的阴影.我的Fragment的xml现在是:

In fact, I have the slide in and the reverse effect with the BackStack, and I overlay my content with a shadow on the left side. The xml of my Fragment it's now:

<RelativeLayout
    android:layout_width="match_parent" android:layout_height="match_parent" >

    <View 
        android:id="@+id/shadowLeft"
        android:layout_width="50dip" android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:background="@drawable/fragment_shadow_left" />

    <ListView  
        android:id="@+id/list"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/shadowLeft" />

</RelativeLayout>

最后,我将FrameLayout设置为主机".上面的这种布局,背景透明,效果很好.
如果有人想编辑或给出更好的答案,我总是在听.希望这对搜索相同效果的人有用.

Finally, I make the FrameLayout to "host" this layout above, with a background transparent and it works perfectly.
If someone wants to edit or give a better answer, I'm always listening. Hope this will be useful for someone who searches the same effect.

这篇关于FragmentTransaction上的CustomAnimations无法用于旧片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆