最近在做一个app时遇到了这样一个问题:
我采用了Navigation侧边栏切换Fragment,其中一个Fragment就需要用到WebView,需求是这个Fragment 里面的WebView滑动的时候Toolbar能够自动隐藏,然而弄了很久也没有办法实现。其中有一个办法能够解决单一页面下的WebView滑动时toolbar 自动隐藏,见https://blog.csdn.net/m5314/article/details/68943869。
他是写了一个继承WebView的控件NestedScrollWebView来实现对CoordinatorLayout+AppBarLayout的支持。不过,由于我的Fragment有很多个,切换Fragment的时候是把欲切换的Fragment应用到id为layout_fragment的一个框架布局上,而只是其中一个需要用到WebView,他这个若是替换掉我的框架布局的话其他几个Fragment都不能显示了。因此不能够使用他写的这个控件来实现对某一个fragment的toolbar隐藏了。
在网上看了很多资料也没能解决我的问题,网上的toolbar隐藏的例子都是利用recyclerPage或者ListView配合CoordinatorLayout、AppBarLayout来实现滑动时Toolbar的隐藏、显示,这明显不符合我的需求。
想要的效果是这样的(不知为何不能显示动图?脑补一下随着滑动webview, toolbar会自动隐藏吧.....:
直接上解决办法:
创建了一个Demo为例,styles.xml的代码如下(注意是NoActionBar):
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
main_activity.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/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary"> <TextView android:text="假装我是toolbar" android:textColor="#ffffff" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center_vertical" android:paddingStart="10dp" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways|snap" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- 这个FrameLayout就是提供给你的那些Fragment显示内容的布局 --> <FrameLayout android:id="@+id/your_fragment" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这里的WebView就是其中一个Fragment的布局文件中的内容,为了展示就简单点写,fragment的主要内容直接放到这里了 --> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> </FrameLayout> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
MainActivity.java的代码如下:
package com.example.asus.demo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView) findViewById(R.id.web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient()); webView.loadUrl("http://www.baidu.com"); } }