微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android监听WebView滑动到底部的两种方式

第一种方式:自定义webView,实现onScrollChanged方法,通过接口的方式暴露出去。

public class ScrollChangeLisWebView extends WebView {
    public ScrollChangeLisWebView(Context context) {
        super(context);
    }

    //实时滑动监控
    //参数l代表滑动后当前位置,old代表原来原值
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mScrollInterface.onSChanged(l, oldt);
    }

    public ScrollInterface mScrollInterface;

    //监控滑动
    public void setonCustomScrollchangelistener(ScrollInterface scrollInterface) {
        this.mScrollInterface = scrollInterface;
    }
    public interface ScrollInterface {
        public void onSChanged(int l, int oldt);
    }

}

使用:

 webView.setonCustomScrollchangelistener(new ScrollChangeLisWebView.ScrollInterface() {
        @Override
        public void onSChanged(int l, int oldt) {
            //WebView的总高度  有缩放比例
            float webViewContentHeight = webView.getContentHeight() * webView.getScale();
            //WebView的现高度
            float webViewCurrentHeight = (webView.getHeight() + webView.getScrollY());
            if ((webViewContentHeight - webViewCurrentHeight) < 10) {  //尽量不要用==0,有时候精度问题导致无法完全相等(自己调试即可)  
              // 已经处于底端
              //.....
            }
        }
    });

常见的坑:
1.如果前端页面height写成100%,会导致webView.getScrollY()一直为0,这个我没碰到,百度有看到别人出现这种;如果出现这种,找前端沟通调整即可。

2.由于原生WebView兼容性很差,如果使用腾讯的X5WebView,webView.getScrollY()也会一直为0,这时候使用webView.getWebScrollY()即可,这种我碰到了。

第二种方式:注入js

  webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                //滑动监听
                String startSave = "\n" +
                        "document.getElementById(\"page-container\").addEventListener('scroll',function () {\n" +
                        "console.log(this.scrollHeight);\n" +
                        "console.log(this.scrollTop);\n" +
                        "console.log(this.clientHeight);\n" +
                        "if(this.scrollHeight-this.scrollTop - this.clientHeight < 50){\n" +
//                      "if(this.scrollHeight-this.scrollTop == this.clientHeight){\n" +     精度计算问题   会有一点误差  无法相等
                        "console.log(\"到达底部\");\n" +
                        "window.java.open(\"到达底部\"); \n" +
                        "}\n" +
                        "});";
                //注入
                webView.loadUrl("javascript:" + startSave);
            }
        });
        
        //注入、反射
        webView.addJavascriptInterface(new Object() {
            @JavascriptInterface
            public void open(String msg) {//滑动到底端
				//.....
				//这里更新UI失败的话,可以用runOnUiThread或者webView.post(new Runnable() {}
            }
        }, "java");

注入js的方式需要与前端约定好调用方法,比如page-container就是前端页面一个标签
可以网页上直接拿代码调试即可。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐