How to hide an ActionBar while scrolling a ListView in Android?

I need to create a GUI with ListView and ActionBar that will hide when scrolling down and should scroll again when scrolling up.

The following guides did not help me:

I need something like this:

enter image description here

+7
android listview android-actionbar appcompat android-design-library
source share
4 answers

If you want to get a list with this behavior, you should:

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

The official 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> 
+14
source share

I would recommend using the new Google support design library.

Include it in your dependencies:

 compile 'com.android.support:design:22.2.0' 

and then use AppBarLayout along with NestedScrollView .

For your Toolbar define an app:layout_scrollFlags="scroll|enterAlways" which says that it will disappear when scrolling and will immediately return if you scroll up (this means you don't have to scroll all the way up).

 <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="fill_vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.NestedScrollView> 

+6
source share

You must use CoordinatorLayout for this task. This is part of the support design library. You can find an example here in the Layout Coordinator and application panels.

+1
source share

Use [CoordinatorLayout]: https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html , which allow co-ordination among child views. it seems to act (AppBarLayout-> scroll) on some view when the observed behavior (ListView-> scroll) is observed in another view.

  • Make Listview nestedScrollingEnabled works for> API 21

     android:nestedScrollingEnabled="true" 
  • Behavior of the trigger pattern for scrolling the application bar.

     android:nestedScrollingEnabled="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" 
  • Any layout (ToolBar / TabLayout / any) that is required to show-hide / scroll, place it inside the AppBarLayout and enable the scroll flag.

     app:layout_scrollFlags="scroll|enterAlways" 
0
source share

All Articles