[英]can`t hide toolbar when webview scrolling
How to hide toolbar, when scrolling webview? 滚动Webview时如何隐藏工具栏? I know, that webview must be in NestedScrollView and CoordinatorLayout, toolbar - in AppBarLayout.But I really cant to do it.
我知道,webview必须在NestedScrollView和CoordinatorLayout工具栏中-在AppBarLayout中,但是我真的做不到。
If anybody can help me, I would be very grateful. 如果有人可以帮助我,我将不胜感激。
activity_main.xml activity_main.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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/main_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_toolbar_size" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tablayout_size"
android:layout_gravity="bottom"
android:background="@color/toolbarColor"
app:tabIndicatorHeight="0dp"
>
<android.support.design.widget.TabItem
android:id="@+id/ti_arrow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_arrow_back" />
<android.support.design.widget.TabItem
android:id="@+id/ti_arrow_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_arrow_forward" />
<android.support.design.widget.TabItem
android:id="@+id/ti_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_update" />
<android.support.design.widget.TabItem
android:id="@+id/ti_inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_inset" />
<android.support.design.widget.TabItem
android:id="@+id/ti_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_star_border" />
</android.support.design.widget.TabLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/custom_tablayout_size"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
toolbar.xml 工具栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_toolbar_size"
android:background="@color/toolbarColor"
android:paddingTop="2dp"
app:layout_scrollFlags="scroll|enterAlways" />
The solutions you have been trying didn't work because because your WebView is inside a NestedScrollView. 您尝试的解决方案无法正常工作,因为您的WebView位于NestedScrollView中。 An OnScrollListener for your WebView won't work too because of that reason.
因此,WebView的OnScrollListener也不起作用。
So use a NestedScrollView listener, then use getSupportActionBar().hide();
因此,请使用NestedScrollView侦听器,然后使用
getSupportActionBar().hide();
and getSupportActionBar().show();
和
getSupportActionBar().show();
to hide or show your toolbar. 隐藏或显示您的工具栏。
Copied from here: https://stackoverflow.com/a/37630070/1860982 从此处复制: https : //stackoverflow.com/a/37630070/1860982
final String TAG = "ScrollPosition";
NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.scrollNextView);
if (nestedScrollView != null) {
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > oldScrollY) {
getSupportActionBar().hide();
Log.i(TAG, "Scroll DOWN");
}
if (scrollY < oldScrollY) {
Log.i(TAG, "Scroll UP");
}
if (scrollY == 0) {
getSupportActionBar().show();
Log.i(TAG, "TOP SCROLL");
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
}
}
});
}
You can do this without any Java code using the design library's CoordinatorLayout and NestedScrollView. 使用设计库的CoordinatorLayout和NestedScrollView,无需任何Java代码即可完成此操作。 Here's how you do it.
这是您的操作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.