【问题标题】:Add ProgressBar Webview in fragment在片段中添加 ProgressBar Webview
【发布时间】:2013-12-07 17:31:53
【问题描述】:

我想在我的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;

    }

}

【问题讨论】:

    标签: android android-fragments webview progress


    【解决方案1】:

    我需要在我的布局中添加progressBar,并在片段本身中显示/隐藏它

    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 顶部有进度条

    【讨论】:

    • 在哪里?? CustomWebChromeClient() 找不到
    • 只需使用 WebChromeClient() - 抱歉,我已经自定义 openFileChoosed 并将其扩展为受保护的类 CustomWebChromeClient 扩展了 WebChromeClient。我更新了答案。
    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2017-06-14
    相关资源
    最近更新 更多