Hiding the Actionbar on Recyclerview/Listview Onscroll

How to hide ActionBar while scrolling ListView in android?

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>

Hide Action Bar on View scrolled

1.implement ViewTreeObserver.OnScrollChangedListener into your class,
2.Write this line into your onCreate "getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);" (without quotes)
3.Now get ActionBar Height for Hide animation as follows -

    final TypedArray mstyled = getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize });
mActionBarHeight = styledAttributes.getDimension(0, 0);
mstyled.recycle();

4.Get Action Bar.

mActionBar = getActionBar();

5.Intilize scrollview.

((ScrollView)findViewById(R.id.parent)).getViewTreeObserver().addOnScrollChangedListener(this);

6.override onScrollChange and mode your method like this.

@Override
public void onScrollChanged() {
float mfloat = ((ScrollView)findViewById(R.id.parent)).getScrollY();
if (mfloat >= mActionBarHeight && mActionBar.isShowing()) {
mActionBar.hide();
} else if ( mfloat==0 && !mActionBar.isShowing()) {
mActionBar.show();
}
}

Hope it Helps.

Android ActionBar hide/show when scrolling list view

To avoid the flickering caused by the readjustment of the layout you can use the overlaying property in the action bar.

https://developer.android.com/training/basics/actionbar/overlaying.html

But this solution causes that the action bar overlays your listview, so you need to add a header with this size:

"?android:attr/actionBarSize"

for example :

mVideosListView = (ListView) getActivity().findViewById(android.R.id.list);
mVideosListView.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.listview_header,null));

Be careful with other elements like navigation drawer which is also affected by this.



Related Topics



Leave a reply



Submit