Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide ActionBar while scrolling ListView in android?

Tags:

I need to create a GUI with a ListView and an ActionBar which will hide when scrolling down and when scrolling up it must reappear.

The following guides didn't help me:

  • https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/
  • https://github.com/ksoichiro/Android-ObservableScrollView

I need something like this:

enter image description here

like image 769
user4789408 Avatar asked Jul 08 '15 10:07

user4789408


1 Answers

If you would like to obtain a list with this behaviour, you should:

  • add the design support library with compile 'com.android.support:design:22.2.0'
  • Use a CoordinatorLayout with a Toolbar where you have to define app:layout_scrollFlags="scroll|enterAlways"
  • Use a RecyclerView instead of a ListView. As described here ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21. In this case you have to use setNestedScrollingEnabled(true);

The official blog post shows this case:

<android.support.design.widget.CoordinatorLayout         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:app="http://schemas.android.com/apk/res-auto"         android:layout_width="match_parent"         android:layout_height="match_parent">       <! -- Your Scrollable View -->     <android.support.v7.widget.RecyclerView             android:layout_width="match_parent"             android:layout_height="match_parent"             app:layout_behavior="@string/appbar_scrolling_view_behavior" />      <android.support.design.widget.AppBarLayout             android:layout_width="match_parent"             android:layout_height="wrap_content">           <android.support.v7.widget.Toolbar                   ...                   app:layout_scrollFlags="scroll|enterAlways">        </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 
like image 138
Gabriele Mariotti Avatar answered Oct 11 '22 23:10

Gabriele Mariotti