Android - 单击列表项时滑动 fragment

标签 android android-fragments android-animation

我正在尝试实现从 fragment1 过渡到 fragment2 的滑动动画,例如 this image .

首先,我尝试使用 set 和 translate 来实现 xml,但是我得到了 RuntimeException “Unknown animator name translate”。

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

其次,我尝试通过使用扩展框架布局的类来解决问题,并添加“getXFraction”和“setXFraction”方法,例如this post

public class SlidingFrameLayout extends FrameLayout
{
    private static final String TAG = SlidingFrameLayout.class.getName();
    public SlidingFrameLayout(Context context) {
        super(context);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public float getXFraction()
    {
        final int width = getWidth();  
        if(width != 0) return getX() / getWidth();  
        else return getX();  
    }

    public void setXFraction(float xFraction) {
        final int width = getWidth();  
        setX((width > 0) ? (xFraction * width) : -9999);  
    }

    public float getYFraction()
    {
        final int height = getHeight();  
        if(height != 0) return getY() / getHeight(); else return getY();   
    }

    public void setYFraction(float yFraction) {
        final int height = getHeight();  
        setY((height > 0) ? (yFraction * height) : -9999);  
    }
}

但是我还是不知道SlidingFrameLayout应该怎么用? 请帮我。 T___T

最佳答案

您需要使用 objectAnimator 而不是 translate。这是一些带有示例的帖子:Animate the transition between fragmentsAndroid Fragments and animation

关于Android - 单击列表项时滑动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810870/

相关文章:

Android 从下到上淡入/淡出动画

android - 在android中放大和缩小动画

android - 使用 BMS Client 时如何以及何时刷新 token ,有没有办法强制刷新?

android - 抽象保存状态 View 模型工厂 : SavedStateProvider with the given key is already registered

android - 如何为android制作一个包含 native 代码和纯java的库

Android - Fragment 中的 ListView 不显示

android - popUpTo 似乎在导航组件中不起作用

android - 构建类似 CSS 的 Android 微调器动画(Stacked ProgressBar 元素)

android - 如何使用 Google Fit Android API 获取步数的数据源?

android - 如何在 Fragment 中显示类似 ListFragment 的进度加载器