【问题标题】:Android - Sliding fragments when click list itemAndroid - 单击列表项时滑动片段
【发布时间】:2013-07-23 12:59:03
【问题描述】:

我正在尝试实现从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

【问题讨论】:

    标签: android android-fragments android-animation


    【解决方案1】:

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

    【讨论】:

    • 但是 objectAnimator 不能像 'android:toXDelta="-100%p"' 那样配置为从屏幕右侧滑动
    【解决方案2】:

    您必须使用 SlidingFrameLayout 作为要在 xml 布局文件中滑动的片段的根视图,例如,如果您的根视图是相对布局:

    <com.company.appname.SlidingFrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </RelativeLayout>
    </com.company.appname.SlidingFrameLayout>
    

    然后使用 objectAnimator,例如向右滑出:

    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:duration="@android:integer/config_mediumAnimTime"
                    android:interpolator="@android:anim/linear_interpolator"
                    android:propertyName="xFraction"
                    android:valueFrom="0"
                    android:valueTo="1.0"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多