简体   繁体   中英

Quick return action bar with android lollipop toolbar

I am using the new toolbar of AppCompat-v21. What I want to do is to hide the action bar when the user starts scrolling downwards and show the action bar again when the user scroll upwards. Its just like the google play app. I also have a tab layout below the toolbar like the google play app. That needs to stay there.

I have tried using codes from google IO app. But it makes the toolbar disappear and the main content does not move up when the toolbar disappears. There remains a void space where to toolbar was before. Maybe its because that implementation is only for list and grid views.

Any idea how to do that?

What you need to do is basically create your own ActionBar theme which sets windowActionBarOverlay to true:

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@android:style/Theme.AppCompat">
    <item name="android:windowActionBarOverlay">true</item>

    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

This does exactly as it sounds - it sits on top of your current view. However, that also means you need to add an additional margin at the top of your main view to account for the ActionBar overlay.

<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    ...
</RelativeLayout>

Once you get your custom ActionBar theme up and running you shouldn't see the extra space anymore. These snippets of code were taken directly from Android Developer Training Guide . Follow the link to get more detailed information on how ActionBarOverlay works.

More easy approach is to use app:layout_scrollFlags .

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

So Toolbar will automatically detect scrolling and begin to hide or show.

PS If you're using ListView , you need to change it to the RecycleView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM