How to hide ActionBar while scrolling ListView in android?
Solution 1
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 defineapp:layout_scrollFlags="scroll|enterAlways"
- Use a
RecyclerView
instead of aListView
. As described hereListView
and theGridView
have the expected behavior with theCoordinatorLayout
only with API>21. In this case you have to usesetNestedScrollingEnabled(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>
Solution 2
I would recommend using the new support design library by Google.
Include it in your dependecies:
compile 'com.android.support:design:22.2.0'
and then use the AppBarLayout
together with NestedScrollView
.
For your Toolbar
define app:layout_scrollFlags="scroll|enterAlways"
, which says it will disappear as you scroll, and come back immediately if you scroll up (meaning 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>
Solution 3
Use [CoordinatorLayout]:https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html, which allow co-oridanation among child views. it's like, act(AppBarLayout->scrolling) on some view when there is a behaviour observed(ListView->scroll) in another view.
-
Make Listview nestedScrollingEnabled, works for >API 21
android:nestedScrollingEnabled="true"
-
Trigger layout behaviour to appbar scrolling.
android:nestedScrollingEnabled="true" app:layout_behavior="@string/appbar_scrolling_view_behavior"
-
Any layout(ToolBar/TabLayout/any), which is required to show-hide/scroll, place it inside AppBarLayout, and enabled scroll flag.
app:layout_scrollFlags="scroll|enterAlways"
Solution 4
you should use CoordinatorLayout
for this task. It is part of the support design library. Here, in the CoordinatorLayout and the app bar section, you can find an example
user4789408
Updated on April 08, 2020Comments
-
user4789408 about 4 years
I need to create a GUI with a
ListView
and anActionBar
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: