安卓:如何将一个碎片向上滑动?

5

我正在尝试为在按钮点击时从底部出现的片段添加动画。

片段布局

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="@color/wallet_holo_blue_light"
        android:layout_gravity="center"
        android:text="This is a fragmentt layout"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

向上滑动.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime">

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="50.0%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toYDelta="30" />
</set>

在这里调用它:
public void onClick(View view) {

           if(view == button ){

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FragmentOnMap hello = new FragmentOnMap();

        fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
        fragmentTransaction.setCustomAnimations(R.animator.slide_up,0);
        fragmentTransaction.commit();
    }


}

Android Fragments和动画展示了如何上下滑动,我只想让片段向上动画。因此我的问题是setcustomanimation()函数的第二个参数。

我尝试在提交之前使用fragmentTransaction.setCustomAnimations(),但没有帮助。

它确实出现在底部,但没有过渡效果。 任何指导都将非常有用。 谢谢


2
可能是Android Fragments and animation的重复问题。 - jakubbialkowski
1个回答

2

在使用add方法之前,您应该先调用setCustomAnimation方法。

另外,如果您在使用Fragment时没有使用支持库(v4),而是使用了app库,请确保使用getSupportFragmentManager方法来修复代码中的所有部分,并且不要忘记使用支持库。

如果您不想进行更改,可以使用下面的代码进行slide_up动画:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="translationY"
    android:valueType="floatType"
    android:valueFrom="1280"
    android:valueTo="0"
    android:duration="@android:integer/config_mediumAnimTime"/>

setCustomAnimation(R.id.slideup,?)的第二个参数将是什么? - AIS
看看我修改过的代码。没有动画效果。使用了你的slide_up。片段只是出现了,没有滑动效果。 - AIS
就像我之前说过的一样,你应该在添加之前设置自定义动画。如果这对你不起作用,那么请将XML文件更改为我添加的代码或者按照我在答案中提到的更改进行操作。 - T.S
我认为你编写的XML只能与支持库一起使用,而我添加的代码可以与你在代码中使用的应用程序库一起使用。 - T.S
valueFrom=1280 在这里是什么意思?我们能改变它吗?我们如何决定这个值? - xaif
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接