【问题标题】:Update the parent view when webView finishes on Android当 webView 在 Android 上完成时更新父视图
【发布时间】:2023-03-19 03:15:02
【问题描述】:

我有一个需要一些时间来加载的 web 视图,所以我有一个进度指示器,它应该在 webView 准备好之前显示。但是,最好的方法是什么?我有:

        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                //mListener.finishedLoadingHtml();
                ProgressBar bar = (ProgressBar) view.findViewById(R.id.article_content_progressIndicator);
                bar.setVisibility(View.GONE);
            }
        });

但这不适用于 bar = null。有没有一种简单的方法可以从回调中访问 progressBar?我还在为 Android 视图层次结构等而苦苦挣扎。

【问题讨论】:

    标签: android webview android-progressbar


    【解决方案1】:

    我是通过javaScript做的
    一开始我在我的webView 上显示progressBar,当页面完全加载时,我使用javaScript 通知java 应该通过这种方式去progressBar

    myWebView.addJavascriptInterface(new JavaScriptInterface(context),
                "jsInterface");
    

    ProgressBar bar = (ProgressBar) view.findViewById(R.id.article_content_progressIndicator);

    public class JavaScriptInterface {
        Context mContext;
    
        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }
    
        @JavascriptInterface
        public void pageLoaded() {
             if(bar!=null)
                 bar.setVisibility(View.GONE);
        }
    
    }  
    

    在html页面内通过javaScript调用方法如下:

    function onPageLoad(){
        jsInterface.pageLoaded();
    }
    

    【讨论】:

    • 我刚刚在类范围内声明了“bar”,这很有效,但我宁愿不必在整个类中声明很多东西。这也是一种有趣的方法。
    【解决方案2】:

    这可能会有所帮助。

    webview.setWebViewClient(new WebViewClient() {
            public void onProgressChanged(WebView view, int progress)   
            {
                //Make the bar disappear after URL is loaded, and changes string to Loading...
                setTitle("Loading...");
                setProgress(progress * 100); //Make the bar disappear after URL is loaded
    
                // Return the app name after finish loading
                if(progress == 100)
                   setTitle(R.string.app_name);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-03
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多