如何在Tab2的webView中添加进度条

3

我想在WebView中添加进度条,但是当我尝试按照这个这个的方法添加进度条时,它们非常混乱,我无法理解,因为我是新手。

我想在我的Tab2活动中添加一个进度条。

package com.freerechargeapp.weebly.free_recharge_app;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;


public class tab2 extends Fragment {



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

    View view = inflater.inflate(R.layout.tab2, container, false);





    WebView webView = (WebView) view.findViewById(R.id.webview1);
    webView.loadUrl("http://google.com");



    return view;



}

这是我的tab2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"


 />

    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/progressBar1"/>




</RelativeLayout>

</LinearLayout>

http://javatechig.com/android/progressbar-while-loading-webview - IntelliJ Amiya
@IntelliJAmiya 太混乱了,我是个新手,我不理解那个。 - Stevenjobs
发布你的 tab2 xml。 - IntelliJ Amiya
你有检查上面的网址吗?进度条在哪里? - IntelliJ Amiya
请检查我下面的答案。 - IntelliJ Amiya
显示剩余4条评论
1个回答

2
尝试这种方法:
public class tab2 extends Fragment {

WebView webView ;
 ProgressBar progressBar;

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

        View view = inflater.inflate(R.layout.tab2, container, false);



        webView = (WebView) view.findViewById(R.id.webview1);
        progressBar = (ProgressBar)view.findViewById(R.id.progressBar1);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://google.com");

        return view;
  }

public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
                progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }


}

我添加了你的代码,但它显示无法解析findViewById(int)。这是它的屏幕截图 screenshot @IntelliJ - Stevenjobs
1
progressBar = (ProgressBar)view.findViewById(R.id.progressBar1); - IntelliJ Amiya
@Stevenjobs 请查看我上面的评论。 - IntelliJ Amiya
@Stevenjobs 等待您的反馈。 - IntelliJ Amiya
你的意思是什么?我不明白。这段代码应该添加到Tab1.java、Tab2.java还是MainActivity.java中?@IntelliJ - Stevenjobs
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接