动画如何隐藏ActionBar并保留标签?

时间:2015-01-13 20:42:12

标签: android android-actionbar android-viewpager android-animation

在Google Play商店应用的第5版中,滚动到内容,滚动操作栏,但选项卡已固定在最顶层。

怎么做?

在滚动之前

enter image description here

滚动后

enter image description here

4 个答案:

答案 0 :(得分:6)

正如其他人所建议的那样,请使用http://quotesondesign.com/api/3.0/api-3.0.json?callback=json

中的ObservableScrollView

尝试将ToolbarSlidingTabStrip放在同一容器中,然后在用户滚动ObservableScrollView时为该容器设置动画,例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">

    <com.github.ksoichiro.android.observablescrollview.ObservableListView
        android:id="@+id/listView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

    <LinearLayout
        android:id="@+id/toolbarContainer"
        android:orientation="vertical"
        android:elevation="10dp"
        android:background="@color/material_deep_teal_200"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>

            <!--Placeholder view, your tabstrip goes here-->
            <View
                android:layout_width="wrap_content"
                android:layout_height="48dp"/>
   </LinearLayout>
</FrameLayout>

然后,当您覆盖ObservableScrollViewCallbacks时,您可以执行以下操作:

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {

    toolbarContainer.animate().cancel();

    int scrollDelta = scrollY - oldScrollY;
    oldScrollY = scrollY;

    float currentYTranslation = -toolbarContainer.getTranslationY();
    float targetYTranslation = Math.min(Math.max(currentYTranslation + scrollDelta, 0), toolbarHeight);
    toolbarContainer.setTranslationY(-targetYTranslation);
}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    float currentYTranslation = -toolbarContainer.getTranslationY();
    int currentScroll = listView.getCurrentScrollY();

    if (currentScroll < toolbarHeight) {
        toolbarContainer.animate().translationY(0);
    } else if (currentYTranslation > toolbarHeight /2) {
        toolbarContainer.animate().translationY(-toolbarHeight);
    } else {
        toolbarContainer.animate().translationY(0);
    }
}

onUpOrCancelMotionEvent内容是为容器设置动画,以防止工具栏只显示/隐藏一半。

这是一个仅供参考的演示视频:https://github.com/ksoichiro/Android-ObservableScrollView

答案 1 :(得分:1)

看看这些链接(特别是第二个链接):

Android google-play-liked listview

ListView with a (half- and or) hidable header

答案 2 :(得分:1)

答案在这里:

https://github.com/ksoichiro/Android-ObservableScrollView:D

这个图书馆非常适合我的案例和其他人

答案 3 :(得分:1)

很高兴你自己回答你的问题;) 这是另一个小提示: 为选项卡使用单独的布局或将它们集成到工具栏中,然后仅在您看到顶部的选项卡时转换工具栏。