在片段中添加ProgressBar Webview

时间:2013-12-07 17:31:53

标签: android android-fragments webview progress

我想在我的class中添加一个进度条,但我只找到了Extend Activity,我使用了抽屉,Fragment

这是我的代码如何添加此progressbar

public class HomeFragment extends Fragment {

    public HomeFragment(){}

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


        View root = inflater.inflate(R.layout.fragment_home, container, false);
        myWebView = (WebView) root.findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://192.168.1.11/MAMP/isn/profil.php");



        return root;

    }

}

1 个答案:

答案 0 :(得分:4)

我需要在布局中添加progressBar,然后从Fragment本身显示/隐藏它

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragmentT4, container, false);
    progressBarT4 = (ProgressBar) rootView.findViewById(R.id.progressBarT4);

    webView = (WebView) rootView.findViewById(R.id.webViewT4);
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBarT4.setVisibility(View.VISIBLE);
            progressBarT4.setProgress(progress);
            if (progress == 100) {
                progressBarT4.setVisibility(View.GONE); // Make the bar disappear after URL is loaded
            }
        }
    });

    progressBarT4.setVisibility(View.VISIBLE);
    webView.loadUrl("http://192.168.1.11/MAMP/isn/profil.php");
}

您需要在类声明中添加几个变量

public class FragmentT4 extends Fragment {
    private View rootView = null;
    private WebView webView;
    private ProgressBar progressBarT4 = null;
   ...

还要注意你的布局并确保你的webView上有progressBar