dnlbaines
dnlbaines

Reputation: 165

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.

Upvotes: 11

Views: 13617

Answers (5)

Kishan Vaghela
Kishan Vaghela

Reputation: 7938

This is work for me.

It is code for GridView. But you can extend ListView instead of GridView.

https://gist.github.com/sakurabird/6868765

Upvotes: -1

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363677

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);
}

Upvotes: 6

jpmcosta
jpmcosta

Reputation: 1780

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.

Upvotes: 4

Takashi Lee
Takashi Lee

Reputation: 145

Views can declare a default Behavior by using the CoordinatorLayout.DefaultBehavior(YourView.Behavior.class) annotation,or set it in your layout files by with the app:layout_behavior="com.example.app.YourView$Behavior" attribute. This framework makes it possible for any view to integrate with CoordinatorLayout.

So I think solution here is override the AppBarLayout.Behavior class

Upvotes: 0

aows
aows

Reputation: 526

It depends on what you are showing in your ViewPager. If you use a list / recyclerview, it should scroll correctly.

Upvotes: 0

Related Questions