Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoordinatorLayout not working

I am attempting to implement a CoordinatorLayout from the newly announced Android Design Support Library and I have used the following code in my XML layout as per the sample here:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

However the action bar does not hide when I scroll down the view. I can't figure out why this is not working.

Edit: As far as I am aware it appears that CoordinatorLayout does not work with ListView/GridView/ScrollViews and only works with RecyclerView and NestedScrollView. Unfortunately simply switching to one of these views is not a possibility for me so I am still looking for a solution.

like image 772
dnlbaines Avatar asked May 31 '15 18:05

dnlbaines


2 Answers

Currenlty not all views have the expected behavior with the CoordinatorLayout.

Your views should implement the NestedScrollView interface and have to handle the nested scroll events.

The RecyclerView and the NestedScrollView (version 22) support this behavior. However you can use also the AbsListView (ListView and GridView) but only with API21+.

Just add something like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     listView.setNestedScrollingEnabled(true);
}
like image 101
Gabriele Mariotti Avatar answered Oct 23 '22 12:10

Gabriele Mariotti


I believe you need to make your ListView implement both ScrollingView and NestedScrollingChild interfaces.

It's not the easiest thing, but you should be able to do it if you look at RecyclerView's source code. It makes use of a NestedScrollingChildHelper and you should be able to do the same.

like image 22
jpmcosta Avatar answered Oct 23 '22 13:10

jpmcosta