CoordinatorLayout does not hide Toolbar on scrolling despite implementing all required parameters
Asked Answered
A

3

7

Here is my setup, i am running a DrawerLayout, within it is a CoordinatorLayout containing an AppBarLayout and a nestedscrollview. I am trying to have the nestedscrollview scroll normally and the Toolbar to get hidden on scrolling down and reppear on scrolling up. Attached within is my XML code. Would appreciate any help.. have read all related questions and implemented their answers without any success.

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

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/admincoordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/adminrelScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer" />

Autolysin answered 23/6, 2015 at 15:52 Comment(4)
For me also same issue, scroll events are not at all working. did u get answer for this?Incitement
same here. This is weirdEpact
@All Anyone found solution for this. It is breaking my head seriously.Ratha
If you found my answer useful, could you accept it? ;)Carmon
C
5

I have had the same issue for a week and tried almost everything to solve it. However I managed to solve the issue.

Where you have something like...

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar"
    app:layout_scrollFlags="scroll|enterAlways" />

...replace this with whatever is in your app_bar.xml layout. For example:

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    style="@style/AppTheme.Toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:layout_scrollFlags="scroll|enterAlways"/>

It seems that for some reason, scrolling with CoordinatorLayout does not work when using the <include> tag.

Carmon answered 19/7, 2015 at 3:28 Comment(1)
I hate so much that this is all I needed this whole time!! XD Thank you!Lip
P
1

I think making use of the new CollapsingToolbarLayout will help… A short description from some very useful exploration of the new Android Design Support Library shows how to wrap a Toolbar in a CollapsingToolbarLayout and customize effects by setting layout_collapseMode.

update

I think adding an onScrollListener to your ListView and showing/hiding the toolbar like this example from this answer:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

listView.setOnScrollListener(new OnScrollListener() {
    int mLastFirstVisibleItem = 0;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {   }           

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if (view.getId() == listView.getId()) {
            final int currentFirstVisibleItem = listView.getFirstVisiblePosition();

            if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                getSupportActionBar().hide();
            } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                getSupportActionBar().show();
            }

            mLastFirstVisibleItem = currentFirstVisibleItem;
        }               
    }
});
Pace answered 24/6, 2015 at 12:56 Comment(1)
isn't a collapsing toolbar layout for the case where you have an (extra?) large toolbar which is restored to normal size on scrolling, what i want to achieve is to simply hide the normal sized toolbar on scrolling.. hope that made my question clear :)Autolysin
G
1

As @Farbod Salamat-Zadehwas said before: CoordinatorLayout does not work when using the <include> tag.
But you can use <include> this way:

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />

Parameter app:layout_scrollFlags="scroll|enterAlways" just should be moved into your app_bar.xml if it acceptable for you

Galan answered 4/8, 2015 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.