I have a page that has a collapsing action bar effect, similar to this: http://xmodulo.com/hide-show-toolbar-scrolling-android.html. But I want the action bar to only show when the list is already showing the first item, not immediately whenever i scroll down. How can I achieve this?
The design library has a component called the AppBarLayout
to accomplish this.
Include the library:
dependencies {
compile 'com.android.support:design:22.2.0'
}
Wrap the Toolbar
in an AppBarLayout
with the scrollFlags set to 'scroll', wrap the entire layout in a CoordinatorLayout
, and add set the layout_behavior of your scrolling view to @string/appbar_scrolling_view_behavior
.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
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"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll"/>
<!-- If you have tabs you can include them below the toolbar -->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<!-- If you use a ViewPager, just add the layout_behavior to it instead of the RecyclerView -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With