隐藏动作栏/工具栏时动画内容

Chr*_*vik 3 android android-5.0-lollipop

我使用了Google IO repo中的代码:https: //github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java

我正在尝试在向上滚动时隐藏工具栏并且它工作得很好,但我的问题是当工具栏动画显示在屏幕外时我的主要内容保持固定.

在R.id.toolbar_actionbar上运行动画的代码是:

view.animate()
        .translationY(-view.getBottom())
        .alpha(0)
        .setDuration(HEADER_HIDE_ANIM_DURATION)
        .setInterpolator(new DecelerateInterpolator());
Run Code Online (Sandbox Code Playgroud)

我已经尝试将工具栏放在带有内容的线性布局中,但它没有改变任何东西.只有当我将操作栏设置为visibility = gone时,内容才会向上移动.

有没有人知道我必须做些什么来使内容与工具栏的翻译动画一起动画?

基本上问题归结为:我如何设置一个视图的位置并使其他视图具有动画效果?

谷歌IO代码似乎只是为工具栏设置动画,其余的内容使用它动画,但我无法让它工作.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_actionbar" />

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adsContainer"
        android:layout_below="@+id/toolbar_actionbar" />

    <LinearLayout
        android:id="@+id/adsContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#000000"
        android:orientation="horizontal" />
</RelativeLayout>
<!-- The navigation drawer -->

<ExpandableListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

Sim*_*mas 7

您可以使用此小片段为视图的高度设置动画:

public void animateHeight(final View view, int from, int to, int duration) {
    ValueAnimator anim = ValueAnimator.ofInt(from, to);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = val;
            view.setLayoutParams(layoutParams);
        }
    });
    anim.setDuration(duration);
    anim.start();
}
Run Code Online (Sandbox Code Playgroud)

调用它看起来像这样:

View view = findViewById(R.id.view);
// Wait for layout to be drawn before measuring it
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        view.removeOnLayoutChangeListener(this);
        animateHeight(view, view.getHeight(), 0, 5000);
    }
});
Run Code Online (Sandbox Code Playgroud)