无法在片段中实现进度条和webview

时间:2017-01-16 11:36:31

标签: android android-progressbar

我想为我的ProgressBar显示WebView,一旦加载完成就会停止,这是我的代码,但是当我致电ProgressBar时,它显示无法解析方法findViewById

package com.fb.jaisonjoseph.facebookbasic;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * A simple {@link Fragment} subclass.
*/
 public class Home_Fragment extends Fragment {

public WebView mwebView;
public Home_Fragment() {

    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_home_, null);
    WebView view=(WebView) rootView.findViewById(R.id.webView);
    view.loadUrl("https://mbasic.facebook.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    return rootView;
}

private class MyWebViewClient extends WebViewClient {
    ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar);

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
        bar.setVisibility(View.VISIBLE);

        super.onPageStarted(view, url, favicon);
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        bar.setVisibility(View.GONE);

        super.onPageFinished(view, url);
        }
    }

 }

fragment_home_.xml

<RelativeLayout 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="com.fb.jaisonjoseph.facebookbasic.Home_Fragment">

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


<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:animationResolution="@integer/abc_max_action_buttons"
    android:clickable="false"
    android:theme="@style/Base.Theme.AppCompat"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

  </RelativeLayout>

1 个答案:

答案 0 :(得分:5)

1.。)WebViewProgressBar位于Fragment布局内,您正在使用inflater初始化return片段布局

2。)因此,片段布局的每个视图都在rootView返回的inflater内,您必须使用rootView初始化属于您Fragment的其他视图1}}

3.。)使用mwebView变量而不是创建局部变量view

4。)shouldOverrideUrlLoading什么也没做,所以添加view.load并返回true

public class Home_Fragment extends Fragment {

    public WebView mwebView;
    ProgressBar bar;

    // ^^^ declare bar 
    public Home_Fragment() {

        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home_, null);
        bar = (ProgressBar) rootView.findViewById(R.id.progressBar);
        // initialize bar

        mwebView = (WebView) rootView.findViewById(R.id.webView);
        mwebView.loadUrl("https://mbasic.facebook.com");
        mwebView.getSettings().setJavaScriptEnabled(true);
        mwebView.setWebViewClient(new MyWebViewClient());
        return rootView;
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
           return true; 
        }

        @Override
        public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
            bar.setVisibility(View.VISIBLE);
            // ^^^ use it as it is

            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            bar.setVisibility(View.GONE);
            // ^^^ use it as it is
            super.onPageFinished(view, url);
        }
    }

}