首页 > 其他分享 >WebView 解决嵌套滑动冲突,丝滑般实现吸顶效果,完美兼容 X5 webview

WebView 解决嵌套滑动冲突,丝滑般实现吸顶效果,完美兼容 X5 webview

时间:2023-02-14 22:12:36浏览次数:50  
标签:实现 嵌套 webview 吸顶 WebView X5

本文首发我的公众号徐公,收录于 Github·AndroidGuide,这里有 Android 进阶成长知识体系, 希望我们能够一起学习进步,关注公众号徐公,5 年中大厂程序员,一起建立核心竞争力

背景

最近项目在开发中,需要实现 WebView 吸顶的效果。刚开始在 Demo 实现的时候,使用的是普通的 WebView。切换到项目的时候,由于使用的是 X5 WebView,在解决过程中。又遇到了一些问题,觉得挺有代表性的,就记录了下来。

如果你也有相似的问题,可以参考这种思路解决。

实现原理简述

讲解之前,我们先来看一下效果图

请添加图片描述

说到嵌套滑动,很多人第一时间都会想到 CoordinatorLayout behavior ,但是 webview 本身并不是 NestedScrollChild 的,无法实现。

于是,我们可以自己实现 NestedScrollChild 接口,去实现嵌套滑动。具体的实现原理,可以参照我的这一篇博客。

【原理篇】WebView 实现嵌套滑动,丝滑般实现吸顶效果,完美兼容 X5 webview

系统 webview 实现吸顶效果

第一步:引入我的开源库

 implementation("io.github.gdutxiaoxu:nestedwebview:0.22")

第二步:借助 CoordinatorLayout behavior 实现吸顶效果

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingDefaultResource">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/colorPrimary"
            android:scaleType="fitXY"
            android:src="@drawable/tangyan"
            app:layout_scrollFlags="scroll" />

    </com.google.android.material.appbar.AppBarLayout>


    <io.github.gdutxiaoxu.nestedwebview.NestedWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>


X5 webview 实现吸顶效果

第一种方式

第一种方式,使用我封装好的 NestedX5WebView,在布局文件中指定 behavior

第一步:引入我的开源库

implementation("io.github.gdutxiaoxu:nestedx5webview:0.22")

第二步:借助 CoordinatorLayout behavior 实现吸顶效果

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingDefaultResource">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:layout_behavior=".behavior.DisableAbleAppBarLayoutBehavior">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/colorPrimary"
            android:scaleType="fitXY"
            android:src="@drawable/tangyan"
            app:layout_scrollFlags="scroll" />

    </com.google.android.material.appbar.AppBarLayout>


    <io.github.gdutxiaoxu.nestedwebview.x5.NestedX5WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

第二种方式

使用腾讯的 WebView,在代码当中动态指定 X5ProxyWebViewClientExtension 即可

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingDefaultResource">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/colorPrimary"
            android:scaleType="fitXY"
            android:src="@drawable/tangyan"
            app:layout_scrollFlags="scroll" />

    </com.google.android.material.appbar.AppBarLayout>


    <com.tencent.smtt.sdk.WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

代理 X5 webview 相关的触摸事件

        val x5CallBackClient = X5CallBackClient(webView.view, webView)
        webView.setWebViewCallbackClient(x5CallBackClient)
        webView.webViewClientExtension = X5ProxyWebViewClientExtension(x5CallBackClient)

更多吸顶效果

使用CoordinatorLayout打造各种炫酷的效果

自定义Behavior —— 仿知乎,FloatActionButton隐藏与展示

NestedScrolling 机制深入解析

一步步带你读懂 CoordinatorLayout 源码

自定义 Behavior -仿新浪微博发现页的实现

ViewPager,ScrollView 嵌套ViewPager滑动冲突解决

自定义 behavior - 完美仿 QQ 浏览器首页,美团商家详情页

源码地址

nestedwebview, 可以帮忙给个 star 哦。

如果觉得对你有所帮助的话,可以关注我我的微信公众号徐公,这里有 Android 进阶成长知识体系, 希望我们能够一起学习进步,关注公众号徐公,一起建立核心竞争力

标签:实现,嵌套,webview,吸顶,WebView,X5
From: https://www.cnblogs.com/jasonxu94/p/17121022.html

相关文章

  • webview2
    webview2简介MicrosoftEdgeWebView2简介搭建开发环境webview2支持的平台还是比较多的,本文对wpf框架进行说明。简单来说:安装Edge浏览器安装webview2(win11都默认......
  • WinForm(十三)WebView2
    WebView是WinForm框架中一个控件,用来对网页信息交互,有时Web自己开发的,有时Web是三方的。下面通过一个例子来看看WebView2的使用。首先看Web的逻辑,是一个商品......
  • WinForm(十三)WebView2
    WebView是WinForm框架中一个控件,用来对网页信息交互,有时Web自己开发的,有时Web是三方的。下面通过一个例子来看看WebView2的使用。首先看Web的逻辑,是一个商品......
  • uniapp webview中动态设置公众号文章标题不显示问题解决
    设置在onLoad中可能会引起偶发性无效。解决方案:1、改写在onReady生命周期中。2、用setTimeout设置延迟。 onReady(){this.timers=setTimeout(()=>{......
  • WPF中使用WebView2控件
    目录WebView2简介概述优势支持的运行时平台进程模型基本使用安装WebView2运行时安装WebView2Sdk打开一个网页导航事件打开一个网页的过程更改url的过程空url进阶使用WPF和W......
  • Android WebView的用法
    WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器,从而非常轻松地展示各种各样的网页。修改activity_main.xml中的代码,如下所示:<LinearLayoutxmlns:andr......
  • package:webview_windows 使用注意
    await_webViewController.loadUrl("F:/language/hbuilderx/code/test/test3.html");await_webViewController.loadUrl("file:///F:/language/hbuilderx/code/test/test3......
  • 【Azure 环境】移动应用 SSO 登录AAD, MSAL的配置为Webview模式时登录页面无法加载
    问题描述移动端集成MASL登录过程中,配置文件中配置项“authorization_user_agent”使用“DEFAULT”可以正常登录,但是改为“WEBVIEW”后就无法登陆,一直处于Loading状态。参......
  • 从webview跳转到小程序报错,'wx' is not defined
    项目有一个需求是从h5中跳转回小程序里,在小程序内报了这个错误'wx'isnotdefined尝试了下面两种方法都没有用npminstallweixin-js-sdk,使用这个包没有效果<scr......
  • webview2报错CoreWebView2 members cannot be accessed after the WebView2 control i
    System.Runtime.InteropServices.COMException(0x8007139F):组或资源的状态不是执行请求操作的正确状态。(异常来自HRESULT:0x8007139F)在Microsoft.Web.WebView2.C......