Hide/show 工具栏
Hide/show Toolbar
在我的应用程序中,我想在列表滚动时 hide/show 工具栏。在我看来,我实现了在 link:
下如何 show/hide 中描述的所有内容
https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/
问题是,滚动列表时,工具栏不是 moving/hiding。
这是 .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>
依赖项如下:
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'
}
如果您想在向下滚动时隐藏工具栏,那么首先检测您的 ListView 或 RecyclerView 的滚动 dy
,然后再从工具栏获取 actionBar 对象。
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);
}
在您的工具栏布局中:
使用 android:layout_height="?attr/actionBarSize"
而不是 android:layout_height="wrap_content"
并删除 android:minHeight="?attr/actionBarSize"
要隐藏工具栏,您可以这样做
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
要再次显示工具栏,请按此操作
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
不幸的是,无法在 ListView 上使用嵌套滚动。
使用粘性 header RecyclerView 代替。
在我的应用程序中,我想在列表滚动时 hide/show 工具栏。在我看来,我实现了在 link:
下如何 show/hide 中描述的所有内容https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/
问题是,滚动列表时,工具栏不是 moving/hiding。
这是 .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>
依赖项如下:
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'
}
如果您想在向下滚动时隐藏工具栏,那么首先检测您的 ListView 或 RecyclerView 的滚动 dy
,然后再从工具栏获取 actionBar 对象。
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);
}
在您的工具栏布局中:
使用 android:layout_height="?attr/actionBarSize"
而不是 android:layout_height="wrap_content"
并删除 android:minHeight="?attr/actionBarSize"
要隐藏工具栏,您可以这样做
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
要再次显示工具栏,请按此操作
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
不幸的是,无法在 ListView 上使用嵌套滚动。 使用粘性 header RecyclerView 代替。