【问题标题】:Slide up and Down animation for Fragment transaction in AndroidAndroid中Fragment事务的上下滑动动画
【发布时间】:2017-03-16 18:11:23
【问题描述】:

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

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

让我们假设用户在 FragmentC 中并且他点击了展开按钮。将会发生的是 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>

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

初始步骤

如果用户单击指示的按钮,将放置一个列表。

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

【问题讨论】:

  • 您在setCustomAnimations 中传递了4 个参数。你只能尝试2个参数.setCustomAnimations(in,out)

标签: java android android-fragments


【解决方案1】:

您可以将动画添加到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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多