【问题标题】:Animate content up when hiding actionbar/toolbar隐藏操作栏/工具栏时动画内容
【发布时间】:2014-12-09 23:06:51
【问题描述】:

我使用了 Google IO 存储库中的代码: 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());

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

有谁知道我必须做什么才能使内容与工具栏的翻译动画一起制作动画?

基本上,问题归结为:如何为一个视图的位置设置动画,并让其他视图用它动画?

Google 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>

【问题讨论】:

    标签: android android-5.0-lollipop


    【解决方案1】:

    您可以使用这个小 sn-p 为视图的高度设置动画:

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

    调用它看起来像这样:

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

    【讨论】:

    • 谢谢!我也设法通过(API 级别 16)做到这一点: LayoutTransition trans = ((RelativeLayout) findViewById(R.id.wrapper)).getLayoutTransition(); trans.enableTransitionType(LayoutTransition.CHANGING);然后设置高度。但真的很想知道 Google IO 代码中的魔力在哪里使它正常工作。
    • @ChristerNordvik 它在 appwidget 包中。他们有自定义类来做这些事情
    • 虽然这行得通,但它给了我一个非常粗略的过渡,寻找更顺畅的工作。谢谢
    【解决方案2】:

    当列表视图向上滚动时,即使在操作栏(appcompat v7 工具栏小部件)向上滑动后,我也遇到了同样的问题。

    Activity 的布局遵循 Christer 的布局。

    这可能是肮脏的修复,也可能是我弄乱了我的布局。但仍然在这里发布以获得更好的解决方案。所以这里是:

    我的内容(片段布局)根元素是这样的:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" >
    ............
    

    我的活动布局如下所示:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <include
            android:id="@+id/toolbar_actionbar"
            layout="@layout/action_bar" />
    </RelativeLayout>
    .........other stuff.........
    

    肮脏的修复是在片段布局中引入一个假工具栏并将其保持在顶部。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    <View
        android:id="@+id/fakeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true" />
    
    .............Other elements like ListView/GridView below this........ 
    

    并且 in onscroll 方法使 fakeToolbar 可见/不可见以及实际的 ToolBar 向上/向下滑动:

        mToolbar.animate().translationY(0).alpha(1)
                .setDuration(HEADER_HIDE_ANIM_DURATION)
                .setInterpolator(new DecelerateInterpolator()).withStartAction(new Runnable() {
    
                    @Override
                    public void run() {
                        fakeToolbar.setVisibility(View.VISIBLE);                        
                    }
                });     
    

    此修复可在滚动时平滑过渡到布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      相关资源
      最近更新 更多