Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

android - How do the animation hiding the ActionBar and keeping tabs?

In version 5 of Google Play Store app, scroll to the content, ActionBar on with scrolling, but the tabs are fixed to get on top.

How to do this?

BEFORE SCROLL

enter image description here

AFTER SCROLL

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others have suggested, use ObservableScrollView from: https://github.com/ksoichiro/Android-ObservableScrollView

Try putting both the Toolbar and the SlidingTabStrip in the same container, then animate that container as the user scrolls the ObservableScrollView, for example:

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

Then when you override the ObservableScrollViewCallbacks you could do something like this:

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

The onUpOrCancelMotionEvent stuff is to animate the container to prevent the toolbar from being only half shown/hidden.

Here's a demo video just for reference: https://drive.google.com/file/d/0B7TH7VeIpgSQSzZER1NneWpYa1E/view?usp=sharing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...