【问题标题】:Android : How to slide up a fragment?Android:如何向上滑动片段?
【发布时间】:2016-10-23 10:29:31
【问题描述】:

我正在尝试将动画添加到按钮单击时出现的片段,从底部开始。

片段布局

 <?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 and animation 展示了如何上下滑动,我只是动画的片段。因此我的问题是 setcustomanimation() 函数的第二个参数

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

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

【问题讨论】:

标签: android android-fragments android-animation


【解决方案1】:

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

另外,您的代码应该有问题,因为您应该使用片段(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"/>

【讨论】:

  • setcustomanimation 的第二个参数是什么? setCustomAnimation(R.id.slideup,?)
  • fragmentTransaction.setCustomAnimations(R.animator.slide_up, 0);如果您使用支持库,则为 R.anim.slide_up。
  • 查看我编辑的代码。没有动画。使用了您的 slide_up。片段刚刚出现。没有滑动效果。
  • 正如我所说,您应该在添加之前设置自定义动画,如果它不适合您,则将 xml 文件更改为我添加的代码或按照我在答案中所说的更改。
  • 是的,这行得通。我的错。我是在添加之后添加的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 2017-06-24
相关资源
最近更新 更多