CoordinatorLayout does not hide the toolbar when scrolling, despite the completion of all necessary parameters

Here is my setup, I run DrawerLayout , inside it is the CoordinatorLayout containing the AppBarLayout and nested scrollview . I try to scroll the nested scrollview normally and the toolbar are hidden when scrolling down and reppear when scrolling up. Nested inside is my XML code. Would thank for any help .. read all related questions and contributed 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" /> 

+6
source share
3 answers

I had the same problem for a week, and I tried to solve almost everything. However, I managed to solve the problem.

Where do you have something like ...

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

... replace this with everything in the app_bar.xml layout. For instance:

 <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> .

+4
source

I think that using the new CollapsingToolbarLayout will help ... A short description from a very useful study of the new Android design support library shows how to wrap the toolbar in CollapsingToolbarLayout and adjust the effects by setting layout_collapseMode.

update

I think adding onScrollListener to the 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; } } }); 
+1
source

As @Farbod Salamat-Zadehwas said: CoordinatorLayout does not work when using the <include> .
But you can use <include> as follows:

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

The app:layout_scrollFlags="scroll|enterAlways" just needs to be moved to your app_bar.xml if it suits you

+1
source

Source: https://habr.com/ru/post/989627/


All Articles