Mohamed Mohy
Mohamed Mohy

Reputation: 61

Toolbar doesn't responsive to scroll events

I want to hide/show the Toolbar when ListView is scrolling. I have CorrdinatorLayout with Toolbar included in. I add the app:layout_scrollFlags="scroll|enterAlways" attribute to the Toolbar. Then I have Listview inside content_min layout with RelativeLayout with app:layout_behavior="@string/appbar_scrolling_view_behavior" attribute. I included the content_main layout inside CoordinatorLayout -Toolbar not responsive to scroll events on listview. Does this Feature work with RecyclerView only and not listview?

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.era.www.onmovie.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<include layout="@layout/content_main" />

content_main layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.era.www.onmovie.MainActivity"
tools:showIn="@layout/activity_main">


<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<TextView
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

Upvotes: 0

Views: 132

Answers (1)

kris larson
kris larson

Reputation: 30985

The scrolling behavior works with RecyclerView but not out-of-box with ListView. This is because the scrolling behavior needs a scrolling component that implements the NestedScrollingChild interface. This interface is implemented by RecyclerView but not ListView.

My recommendation would be to convert your ListView adapter to a RecyclerView adapter and use RecyclerView.

Supposedly there are some ways to put a ListView inside a NestedScrollView which will give the scrolling behavior. I don't recommend this, but if you search SO you can find some solutions for wrapping your ListView inside a NestedScrollView and getting the toolbar to scroll.

Upvotes: 0

Related Questions