我正在尝试获取一个显示进度条,直到加载网页。如果用户没有互联网连接,而不是文字或吐司...但我无法让它工作..搜索整个网站。我认为很容易在课程中添加进度条以消耗活动... 这是我的班级和布局
V
和我的布局
package com.nirav.rpta.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.nirav.rpta.R;
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
WebView heroespage = (WebView) rootView.findViewById(R.id.webview);
WebSettings webSettings = heroespage.getSettings();
webSettings.setJavaScriptEnabled(true);
heroespage.loadUrl("http://google.co.in/");
return rootView;
}}
答案 0 :(得分:0)
请在下面的代码中查看您想要的内容。
您的布局
<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.nirav.rpta.fragments.OneFragment">
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
您的Java文件应如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
WebView heroespage = (WebView) rootView.findViewById(R.id.webview);
// Resources
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar2);
WebSettings settings = heroespage.getSettings();
settings.setJavaScriptEnabled(true);
heroespage.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
// Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.getVisibility() == View.VISIBLE) {
progressBar.setVisibility(View.GONE);
}
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Log.e(TAG, "Error: " + description);
Toast.makeText(WebViewScreen.this, "You are offline.",
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.VISIBLE);
}
});
heroespage.loadUrl(URL);
return rootView;
}}
答案 1 :(得分:0)
将ProgressBar添加到布局文件中:
<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.nirav.rpta.fragments.OneFragment">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"/>
</RelativeLayout>
现在在代码中:
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
WebView heroespage = (WebView) rootView.findViewById(R.id.webview);
ProgressBar loading = (ProgressBar) rootView.findViewById(R.id.progress);
heroespage.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loading.setVisibility(View.GONE);
heroespage.setVisibility(View.VISIBLE);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Show error toast
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// Show error toast
}
});
heroespage.loadUrl("http://google.co.in/");
答案 2 :(得分:0)
You must update your layout
<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.nirav.rpta.fragments.OneFragment">
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/progbar"/>
</RelativeLayout>
And then find id of progressbar ProgressBar prog = view.findViewById(R.id.progbar);
to check internet connection use this method
public boolean isConnectingToInternet( Context _context){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
add this code in your oncreateview
heroespage.setWebViewClient(new WebViewCLient(){
public void onPageLoadFinished(){
progbar.setvisibility(View.Gone);
});
if(isconnectingInternet(getActivity()){
heroespage.loadUrl("http://google.co.in/");
}else{
// show your toast here
}
答案 3 :(得分:0)
隐藏/显示进度条你可以使用提供onPage strated和onPageFinished方法的setWebViewClient
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Show Progress bar
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// hide Progress bar
}
});
要检查没有互联网连接,您可以使用以下代码
public static boolean isNetConnected() {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
}
return false;
}
在加载网址之前使用上述方法
if(isNetConnected()) {
heroespage.loadUrl("http://google.co.in/");
} else {
Toast.makeText(getActivity() ,"No internet connection",Toast.LENGTH_SHORT).show();
}