Quick return bar with toolbar for android lollipop

I am using the new AppCompat-v21 toolbar. I want to do this to hide the action bar when the user starts scrolling down and show the action bar again when the user scrolls up. It looks like a Google Play app. I also have a tab below the toolbar, like the google play app. It should stay there.

I've tried using codes from Google IO app. But this makes the toolbar disappear and the main content doesn't move when the toolbar disappears. This leaves an empty space where the toolbar used to be. Perhaps because this implementation is only for list and grid view.

Any idea how to do this?

+3


source to share


2 answers


What you need to do is create your own ActionBar theme that sets the 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 happens exactly as it appears - it sits on top of your current view. However, this also means that you need to add extra headroom at the top of your main view to account for the ActionBar overlap.



<!-- 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 create your own ActionBar theme, you should no longer see the extra space. These code snippets were taken directly from the Android Developer 's Guide for Developers . Follow the link for more details on the ActionBarOverlay action.

+2


source


An easier 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, the toolbar will automatically detect scrolling and start hiding or showing.

PS If you are using ListView

then you need to change it toRecycleView

0


source







All Articles