在WebView中想要使用FAB,如果你想向上滑动的时候隐藏FAB,那么需要再WebView外面套一个ScrollView!

原因之前也分析过,和为什么ListView不能让ToolBar、Tab隐藏一样,CoordinatorLayout里面没有一个「可滑动」的组件。

如果没有这个ScrollView,那你的ToolBar甚至都不能滑动隐藏。

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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_scrollFlags="scroll|enterAlways"> <android.support.v7.widget.Toolbar
android:id="@+id/toobar_custom"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" /> <com.gc.materialdesign.views.ProgressBarIndeterminate
android:id="@+id/web_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"> <WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_favorite_white_24dp"
app:layout_behavior="com.drunkpiano.zhihuselection.utilities.ScrollingFavoriteFABBehavior" /> </android.support.design.widget.CoordinatorLayout>

Behavior(sdk 22+):

/**
* Created by DrunkPiano on 16/5/2.
*/
public class ScrollingFavoriteFABBehavior extends FloatingActionButton.Behavior{ public ScrollingFavoriteFABBehavior(Context context, AttributeSet attrs) {
super();
} @Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
// Ensure we react to vertical scrolling
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
} @Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View target, final int dxConsumed, final int dyConsumed,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
// User scrolled down and the FAB is currently visible -> hide the FAB
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// User scrolled up and the FAB is currently not visible -> show the FAB
child.show();
}
}
}

有时候我觉得开发安卓就是经验的堆积。

May 2nd

参考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0407/4126.html

如果你使用WebView+FloatingActionButton的更多相关文章

  1. webview页面缩放 & 自适应

    0.webview页面自适应: // 1.LayoutAlgorithm.NARROW_COLUMNS : 适应内容大小// 2.LayoutAlgorithm.SINGLE_COLUMN:适应屏幕, ...

  2. 转-- WebView使用实例(html5、文件下载和远程URL)

    在Android中有WebView Widget,它内置了WebKit引擎,同时,WebKit也是Mac OS X的Safari网页浏览器的基础.WebKit是一个开源的浏览器引擎,Chrome浏览器 ...

  3. WebView内容自适应

    webview页面自适应 //适应内容大小 start webSetting.setUseWideViewPort(true); webSetting.setLayoutAlgorithm(WebSe ...

  4. webview自总结

    2,webview ---- 运行时不调用系统自带浏览器: 1,安卓webview post传值问题: 11,WebView基本功能(html5.文件下载和远程URL) 10,webview--网络超 ...

  5. 安卓开发笔记——WebView组件

    我们专业方向本是JAVA Web,这学期突然来了个手机App开发的课设,对于安卓这块,之前自学过一段时间,有些东西太久没用已经淡忘了 准备随笔记录些复习笔记,也当做温故知新吧~ 1.什么是WebVie ...

  6. XCode下Swift – WebView IOS demo

    简介 我今天用Mac升级了XCode到8.1,Swift版本应该到了swift3,按网上的demo写webview的例子,报一堆错,整了一天才搞定,不想其他人踩坑了! XCode8.1 ,swift3 ...

  7. Android Studio 学习笔记(五):WebView 简单说明

    Android中一个用于网页显示的控件,实际上,也可以看做一个功能最小化的浏览器,看起来类似于在微信中打开网页链接的页面.WebView主要用于在app应用中方便地访问远程网页或本地html资源.同时 ...

  8. android常见问题(一)

    一:文本的颜色选择器: 在res目录下面创建color文件夹,在color文件夹下面创建font_style_colors.xml文件<?xml version="1.0" ...

  9. Android一些问题

    1.wait()与sleep() wait()方法会释放占有的对象锁,当前线程进入等待池,释放cpu, 而其他正在等待的线程即可抢占此锁,获得锁的线程即可运行程序: sleep()方法则表示,当前线程 ...

随机推荐

  1. ios NavigationViewController跳转以及返回传值

    (一)使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面.这种话.下一页面相同在NavigationViewContr ...

  2. maven;spring;pom

    [说明]因为对环境配置文件理解的不充分,遇到问题经常是无法独自解决,特别是maven和javaweb的转换,也是糊里糊涂的,今天就又出问题了. [说明] 一:今日完成 1)任务二的效果展示看的我一脸懵 ...

  3. HTML使用post方式提交中文内容出现乱码的错误解决方式

    今天在做一个例子的时候,使用post方式提交表单,如果有中文的话,在另一个页面显示出来的时候,总是会出现乱码: 但是将提交方式改为get的时候,就不会出现这种错误. 详细错误见下面图片和代码. HTM ...

  4. 解决 ie 返回json提示下载 ResponseEntity方法

    js 配合java  springMVC后台,成功后返回消息,chrom ff都正常,只有IE提交后返回的JSON提示下载,查看类型 application/json  google后发现原来是IE不 ...

  5. java拾遗3----XML解析(三) StAX PULL解析

    使用PULL方式解析XML: Pull是STAX的一个实现 StAX是The Streaming API for XML的缩写,一种利用拉模式解析(pull-parsing)XML文档的API StA ...

  6. ArcGIS API for js InfoWindow

    说明:有关该示例中怎么引用部署在iis上的离线arcgis api请参考我前面的博文 1.运行效果 2.HTML代码 <!DOCTYPE html> <html> <he ...

  7. META-INF中的INF的意思

    1 META是元的意思,比如meta data,元数据. 2 什么是meta data 元数据就是描述其它数据的数据,比如web page中的meta data,包括关键字,对该网页的描述等等. 3 ...

  8. Linux环境安装Nginx详细步骤

    1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了yum install gcc patch libffi-devel python-devel  zlib-devel bzip ...

  9. ARDUINO使用GPRS发送GPS数据到OneNet测试

    功能: 测试把固定的GPS数据发送到OneNet平台 调试途中碰到的问题 ARDUINO不支持sprintf的double打印,只能转换为char字符串然后再%s打印 #include <Tim ...

  10. Yii2 提供可以用属性的方式去获取类的一个方法

    刚开始用 Yii 的小朋友可能对下面的写法非常疑惑: public function actionIndex() { $user = User::find()->where(['name'=&g ...