通过动画交换活动中的片段 [英] swap fragment in an activity via animation

查看:22
本文介绍了通过动画交换活动中的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过动画交换活动中的两个片段.假设 PageA 用于片段 A 和屏幕左侧,而 PageB 用于片段 B,即在屏幕右侧.现在我希望当我单击 pageA 上的按钮时,PageA 将移动到屏幕右侧并带有一些过渡动画.

I want to swap two fragment in an activity via animation.Suppose PageA is for fragement A and left side on the screen and PageB is for fragment B i.e. on the right side of the screen. Now i want that when i click a button on pageA then PageA will move to the right side of the screen with some transition animation.

我尝试了下面的代码来替换位置

I tried the below code to replace the position

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new FragB());
fragmentTransaction.commit();

寻找线索.

提前致谢.

推荐答案

老问题,你可能已经弄清楚了,但供以后参考:

Old questiion and you probably already figured it out, but for future reference:

这是通过代码替换片段时用来设置自定义动画的内容:

here's what you use to set a custom animation when you replace a fragment via code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, newFragment, "fragment");
// Start the animated transition.
ft.commit();

以下是 slide_in_left 动画的示例:

Here is an example of the slide_in_left animation:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-100%"
   android:toXDelta="0"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>

请注意,如果您使用的是兼容库,则这是动画.相反,如果您使用的是原生支持 FragmentManager 的 SDK,那么您的动画将如下所示:

Note that this is the animation if you are using the compatibility library. Instead if you are using and SDK with native support for the FragmentManager then your animation will look like this:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>

这是因为兼容库不支持新的 objectAnimator 类型,而是只实现了旧的动画框架.

This is because the compatibility library does not support the new objectAnimator type and instead only implement the old animation framework.

这篇关于通过动画交换活动中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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