【问题标题】:OnProgressChanged is never usedOnProgressChanged 从未使用过
【发布时间】:2018-04-05 15:49:32
【问题描述】:

我正在尝试设置一个主要显示 progress barwebview,一旦网站完全加载,progress bar 应该会消失。我正在关注这段代码:

https://stackoverflow.com/a/8467430/7803533

问题是 OnProgressChanged 方法从未使用过:

    public class HUB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_hub, container, false);

        final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);

        WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient(){

            public void onProgressChanged(WebView view, int progress){
                if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                    progressBar.setVisibility(ProgressBar.VISIBLE);
                }

                progressBar.setProgress(progress);
                if(progress == 100){
                    progressBar.setVisibility(ProgressBar.GONE);
                }
            }
        });
        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl("https:10.10.1.40/index.html");
        return rootView;
    }
}

布局:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="sykes.moodleapp.HUB">

        <!-- TODO: Update blank fragment layout -->

        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:padding="2dip"/>

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webview"
            ></WebView>

    </FrameLayout>

【问题讨论】:

    标签: java android android-fragments android-webview android-progressbar


    【解决方案1】:

    通过添加以下行将您的进度条可见性更改为 xml 中的GONE

    android:visibility="gone"
    

    因为你的代码这个条件if(progress &lt; 100 &amp;&amp; progressBar.getVisibility() == ProgressBar.GONE)只会在进度小于100并且可见性是`View.GONE:时才会导致true

    其他方式

    在您获得对进度条的引用之后,在其旁边添加一行 ViewGONE 这样:

    final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar); 
    progressBar.setVisibility(View.GONE); //Add this line
    

    【讨论】:

      【解决方案2】:

      为了将来参考,WebViewClient 没有 onProgressChanged() 方法,WebChromeClient 有。

      我最终都使用了它们,因为我希望能够使用onReceivedSslError() 显示 SSL 错误。

      public class HUB extends Fragment {
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      
      
          // Inflate the layout for this fragment
          View rootView = inflater.inflate(R.layout.fragment_hub, container, false);
      
          final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);
      
          WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
          mWebView.setWebChromeClient(new WebChromeClient(){
          // SSL error handler
          });
      
          mWebView.setWebViewClient(new WebViewClient(){
      
              public void onProgressChanged(WebView view, int progress){
                  if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                      progressBar.setVisibility(ProgressBar.VISIBLE);
                  }
      
                  progressBar.setProgress(progress);
                  if(progress == 100){
                      progressBar.setVisibility(ProgressBar.GONE);
                  }
              }
          });
          mWebView.clearCache(true);
          mWebView.clearHistory();
          mWebView.getSettings().setJavaScriptEnabled(true);
          mWebView.getSettings().setDomStorageEnabled(true);
          mWebView.loadUrl("https:10.10.1.40/index.html");
          return rootView;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多