Reputation: 694
In my application, I want to hide/show the toolbar when the list is scrolling. In my opinion, I implemented everything like described in this how to show/hide under link:
https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/
The problem is, when the list is scrolled, the toolbar isn't moving/hiding.
Here is the code of the .xml:
<?xml version="1.0" encoding="utf-8"?>
<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.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:titleTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/lvRef"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Here are the dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'se.emilsjolander:stickylistheaders:2.7.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
}
Upvotes: 1
Views: 2685
Reputation: 2848
If you want to hide the toolbar on scroll down then first detect the scroll dy
of your ListView or RecyclerView and before that get the actionBar object from toolbar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//in on scroll listener of your view check for dy
if (dy > 0) {
//detecting scroll up action
toolbar.setVisibility(View.GONE);
} else {
//detect scrolldown action dy<0
toolbar.setVisibility(View.VISIBLE);
}
Upvotes: 1
Reputation: 176
Unfortunately, there is no way to get nested scrolling working on ListView. Use sticky header RecyclerView instead.
Upvotes: 2
Reputation: 13555
To hide the toolbar you can do like that
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
To Show the toolbar again do like this way
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
Upvotes: 0
Reputation: 606
In your toolbar's layout:
use android:layout_height="?attr/actionBarSize"
instead of android:layout_height="wrap_content"
and remove android:minHeight="?attr/actionBarSize"
Upvotes: 1