Android:如何向上滑动片段?

AIS*_*AIS 5 android android-animation android-fragments

我试图将动画添加到从底部单击按钮时出现的片段中。

片段布局

 <?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>
Run Code Online (Sandbox Code Playgroud)

滑动up.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>
Run Code Online (Sandbox Code Playgroud)

在这里调用:

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();
    }


}
Run Code Online (Sandbox Code Playgroud)

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

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

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

T.S*_*T.S 2

你应该setCustomAnimation之前add,仅此而已。

另外,您的代码应该有问题,因为您应该使用片段(v4)的支持库而不是应用程序,并在使用片段时调用 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"/>
Run Code Online (Sandbox Code Playgroud)