Android:如何在将数据加载到 WebView 时显示进度条?
Posted
技术标签:
【中文标题】Android:如何在将数据加载到 WebView 时显示进度条?【英文标题】:Android: How can i show progress bar while loading data into WebView? 【发布时间】:2012-03-11 15:00:40 【问题描述】:如何在将数据加载到我的 webview 时显示进度条? 我的代码:
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_PROGRESS);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
this.setProgressBarVisibility(true);
setContentView(R.layout.layout_article);
WebView webview = (WebView) findViewById(R.id.webView);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient()
public void onProgressChanged(WebView view, int progress)
activity.setProgress(progress * 100);
);
webView.loadUrl("http://google.com");
【问题讨论】:
您需要将上传移至单独的线程,以释放主 UI 线程的处理。您还必须决定是显示微调器还是显示进度条。 最好的方法是 Paresh 建议的,但您必须使用WebViewClient
,使用此技术您不必查看从服务器获取了多少数据。跨度>
希望以下链接对您有所帮助:***.com/questions/9171510/…
android WebView progress bar的可能重复
【参考方案1】:
在调用 loadData() 函数之前,只需尝试显示 ProgressDialog 或将 ProgressBar 放入布局中。
在 WebViewClient 中,只需关闭()progressDialog 或使 ProgressBar 不可见。
例如:
// when finish loading page
public void onPageFinished(WebView view, String url)
if(mProgress.isShowing())
mProgress.dismiss();
仅供参考,仅在您完成 Web 客户端设置后调用 loadData() 或 loadURL()。
查看此示例:Load WebView with ProgressDialog
【讨论】:
WebChromeClient class
没有onPageFinished() method
,而是在WebViewClient
中
你还是写了WebClient
它是WebViewClient
developer.android.com/reference/android/webkit/…, java.lang.String)【参考方案2】:
请尝试以下代码,
ProgressDialog progDailog = ProgressDialog.show( context,"Process ", "Loading Data...",true,true);
new Thread ( new Runnable()
public void run()
// your data loading code goes here
).start();
Handler progressHandler = new Handler()
public void handleMessage(Message msg1)
progDailog.dismiss();
【讨论】:
【参考方案3】:public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setjavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient()
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url)
return false;
@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon)
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
@Override
public void onPageFinished(final WebView view, final String url)
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
);
webView.loadUrl("http://google.com");
还有R.layout.activity_main
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<WebView
android:id="@+id/webview"
android:layout_
android:layout_ />
<ProgressBar
android:id="@+id/progress"
android:layout_
android:layout_
android:visibility="gone"
android:layout_centerInParent="true"/>
</RelativeLayout>
【讨论】:
【参考方案4】:首先制作自定义 webview 类并像这样在 xml 上使用它,
<com.app.ui.views.CustomWebView
android:id="@+id/webView"
android:layout_
android:layout_
>
</com.app.ui.views.CustomWebView>
确保,高度不应该是固定的,应该是“wrap_content”
现在像这样制作自定义 webview 类,
public class CustomWebView extends WebView
private ProgressBar progressBar;
private Context context;
private LinearLayout loaderLayout;
public CustomWebView(Context context)
super(context);
this.context = context;
initProgressBar();
loadUrlWithData();
public CustomWebView(Context context, AttributeSet attrs)
super(context, attrs);
this.context = context;
initProgressBar();
public CustomWebView(Context context, AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
this.context = context;
initProgressBar();
private void initProgressBar()
progressBar = new ProgressBar(context);
progressBar.setIndeterminate(false);
private void setLoading(boolean isLoading)
if (isLoading)
progressBar.setVisibility(View.VISIBLE);
loaderLayout = new LinearLayout(context);
loaderLayout.setGravity(Gravity.CENTER);
loaderLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
loaderLayout.setLayoutParams(parentLayoutParams);
LinearLayout.LayoutParams progressBarLayoutParams = new
LinearLayout.LayoutParams(75, 75);
progressBar.setLayoutParams(progressBarLayoutParams);
if (progressBar.getParent() != null)
((ViewGroup) progressBar.getParent()).removeView(progressBar);
loaderLayout.addView(progressBar);
// add this loaderLayout to your parent view in which your webview is added
else
progressBar.setVisibility(View.GONE);
private void loadUrlWithData()
setLoading(true);
//Load your html data with url here
//When the content will be loaded, the webview will change it's height
@Override
protected void onSizeChanged(int w, int h, int ow, int oh)
super.onSizeChanged(w, h, ow, oh);
if (h > 400)
setLoading(false);
【讨论】:
以上是关于Android:如何在将数据加载到 WebView 时显示进度条?的主要内容,如果未能解决你的问题,请参考以下文章
Android:将 sqlite 数据库内容加载到 webview
从原生android发送数据到webview javascript页面重新加载网页