在Android中为Fragment事务上下滑动动画

时间:2017-03-16 08:27:56

标签: java android android-fragments

我正在Android中开发一款应用。我正在使用流程从用户那里获取信息。为了构建流程,我使用了很少的片段。有五个步骤,我使用五个片段。我正在使用另一个片段来显示他使用列表视图保存的先前记录。在我的活动中,我使用名为Expand的按钮。按钮Expand用于显示使用片段的先前记录。当用户单击按钮展开时,将发生片段,并且“展开”按钮文本将设置为“隐藏”。当按钮文本为“隐藏”时,如果用户再次单击该按钮,则将从堆栈中删除该片段,并显示添加到后台堆栈的先前片段。

例如,让我们假设我有五个名为FragmentA,FragmentB,FragmentC,FragmentD,FragmentE的片段和另一个名为ProjectRowsFragment的片段,它们将用于显示先前在名为Expand的按钮的click事件中保存在ListView中的记录。

让我们假设用户在FragmentC中并且他点击了Expand按钮。将会发生什么是FragmentC将被替换并且将添加ProjectRowsFragment。如果用户再次单击该按钮,则将替换ProjectRowsFragment,并且FragmentC将从后端堆栈进入。如果它是FragmentD,那么它将被替换,并且将添加ProjectRowsFragment,如果用户再次点击该按钮,ProjectRowsFragment将被替换,FragmentD将从后端堆栈进入。

我已完成交易。

我想要的是我希望添加动画,同时显示和替换ProjectRowsFragment(我用来显示记录的片段)。当它显示时它将从顶部向下滑动,然后当它从后面的堆叠中移除时它将向上滑动。

经过多次尝试后,我完成了下滑效果,但是如何才能获得上滑动画。

这是我的代码。

fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_from_top, 0, R.animator.slide_in_from_bottom, 0);
fragmentTransaction.replace(R.id.fragment_container, ProjectRowsFragment.newInstance(this.projectId));
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
projectRowsExpanded = true;

slide_in_from_top.xml文件是

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<objectAnimator
    android:duration="600"
    android:propertyName="y"
    android:valueFrom="-1280"
    android:valueTo="0"
    android:valueType="floatType" />
</set>

这里我有三个可视化的图像

初始步骤

enter image description here

如果用户点击指示按钮,则会放置一个列表。

enter image description here

如果用户再次点击指示的按钮。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将片段容器视图传递给以下函数以展开和折叠,而不是将自定义动画应用于片段,您可以向FrameLayout添加动画:

FrameLayout v = (FrameLayout) findViewById(R.id.fragment_container);
expand(v); //To Expand
collapse(v); //To Collapse


public static void expand(final View v) {
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int) (targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}