简体   繁体   English

进度对话框不会消失,似乎会中断URL加载

[英]Progress Dialog doesn't disappear and seems to interrupt URL loading

I have a WebView that when it loads a site there will be a ProgressDialog showing. 我有一个WebView,它在加载网站时会显示一个ProgressDialog。 It works fine when you first open the app but when you click a link it just appears and doesn't go away until you click outside it. 首次打开该应用程序时,它工作正常,但是当您单击链接时,它只会显示,直到您在其外部单击时它才会消失。 It also seems like the link the user clicked doesn't get loaded but when I remove the code for the ProgressDialog it works fine. 看来用户单击的链接没有被加载,但是当我删除ProgressDialog的代码时,它可以正常工作。

Thanks in advance! 提前致谢!

package *package URL*;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class TabFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,                 Bundle savedInstanceState) {
    View mainView = (View) inflater.inflate(R.layout.fragment_tab_fragment1, container, false);

    WebView webViewNews = (WebView) mainView.findViewById(R.id.webViewNews);

    webViewNews.getSettings().setJavaScriptEnabled(true);
    webViewNews.setVerticalScrollBarEnabled(false);
    webViewNews.setHorizontalFadingEdgeEnabled(false);

    final ProgressDialog pd=new ProgressDialog(getActivity());
    webViewNews.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            pd.show();
            return true;
        }
    });
    // this is necessary for "alert()" to work
    webViewNews.setWebChromeClient(new WebChromeClient());

    // load a page to get things started
    pd.show();

    webViewNews.loadUrl("http://google.com");
    return mainView;
}

}

I replaced the ShouldOverrideUrlLoadning with onPageStarted and onPageFinished. 我用onPageStarted和onPageFinished替换了ShouldOverrideUrlLoadning。 I also decided to change the ProgressDialog with a Spinner for aesthetic reasons. 出于美学原因,我还决定使用Spinner更改ProgressDialog。 Here are instructions on how I did it. 以下是有关如何操作的说明。

You add a spinner (also a wrapper for the spinner but not necessary) in the layout. 您在布局中添加了一个微调器(也是微调器的包装,但不是必需的)。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#cc111111"
    android:id="@+id/pbWrapper">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:elevation="1000dp"/>
</RelativeLayout>

Then add this BEFORE your onCreate 然后在onCreate之前添加它

private ProgressBar mPbar = null;
private RelativeLayout mPbarWrap = null;

Then you need to find the spinner (and wrapper) 然后,您需要找到微调器(和包装器)

mPbar = (ProgressBar) mainView.findViewById(R.id.progressBar);
mPbarWrap = (RelativeLayout) mainView.findViewById(R.id.pbWrapper);

Then finally show and hide the spinner (and wrapper) based on if there is a change in the WebView 然后根据WebView中是否有更改,最后显示和隐藏微调器(和包装器)

webViewNews.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView mainView, String url, Bitmap favicon) {
            mPbar.setVisibility(View.VISIBLE);
            mPbarWrap.setVisibility(View.VISIBLE);
        }

        public void onPageFinished(WebView view, String url) {
            mPbar.setVisibility(View.GONE);
            mPbarWrap.setVisibility(View.GONE);
        }
    });

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM