美文网首页
Webview开发之滚动判断

Webview开发之滚动判断

作者: FreedApe | 来源:发表于2018-11-27 15:28 被阅读0次

最近项目上有一个关于webview的需求

具体内容
在Webview页面内容全部显示且滚动到最底部的时候,显示特定的UI界面。

Show Time

  • 先自定义一个Webview
public class TermsWebView extends WebView {

    public interface ScrollInterface {
        void onSChanged(int scrollX, int scrollY, int oldScrollX, int oldScrollY);
    }

    ScrollInterface mScrollInterface;

    public void setOnCustomScrollChangeListener(ScrollInterface mInterface) {
        mScrollInterface = mInterface;
    }

    public TermsWebView(Context context) {
        super(context);
    }

    public TermsWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TermsWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mScrollInterface != null){
            mScrollInterface.onSChanged(l, t, oldl, oldt);
        }
    }
}
  • 之后在XML布局文件中使用自定义的Webview
<xxx.xxx.xxx.TermsWebView
                android:id="@+id/web_view_terms"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layerType="software"/>
  • 在设置WebViewClient的地方做如下设置
  mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                isLoadError = true;  // webview加载失败 的flag(全局变量)
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                isLoadError = true; // webview加载失败 的flag
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                isLoadError = false; // webview开始加载时的默认值
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // webview加载完成后
                if (mWebView != null) {
                    mWebView.setOnCustomScrollChangeListener((scrollX, scrollY, oldScrollX, oldScrollY) -> {
                        if (!mWebView.canScrollVertically(1) && !isLoadError) {
                            // 显示特定的UI界面或者特殊处理
                        }
                    });
                }
            }
        });
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(false);
        mWebView.getSettings().setSupportMultipleWindows(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.setOnCustomScrollChangeListener(null);  // 将listener置空 

相关文章

网友评论

      本文标题:Webview开发之滚动判断

      本文链接:https://www.haomeiwen.com/subject/chwgqqtx.html