2017-01-16 106 views
-5

的WebView沿實施進度條我想顯示ProgressBarWebView,一旦裝載完成後,應立即停止,這裏是我的代碼,但是當我打電話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> 
+0

請張貼布局文件代碼 – avinash

+2

如果進度條是在一些其他的佈局,那麼你需要的是充氣佈局,如果進度是相同的佈局,然後用rootView參考其初始化。 –

+0

佈局文件代碼包括@avinash –

回答

5

1)您WebViewProgressBar是內部Fragment佈局和您使用inflater初始化和return你的片段佈局

2)所以你的片段佈局的每一個觀點是inflater返回rootView內,你必須使用rootView來初始化你的其他觀點屬於你的Fragment

3)使用,而不是創建局部變量view

4)shouldOverrideUrlLoading是無爲mwebView變量,所以加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); 
     } 
    } 

} 
+2

這應該是可以接受的答案 –

+0

偶數頁結束後加載進度,你可以爲這個答案@IntelliJAmiya –

+0

@Jaison_Joseph你'shouldOverrideUrlLoading'有空實現更具體,以便看看在我的答案中執行 –