gpt4 book ai didi

android - 带有 WebView 的 ViewPager 中的 ProgressBar

转载 作者:行者123 更新时间:2023-11-29 00:32:24 26 4
gpt4 key购买 nike

我在 ViewPager(v4 支持)中有一个 webview,我想在应用程序加载时放置一个进度条。继承人我到目前为止,但它不工作。基本上我让它指向我的 WebVIew 布局文件中的进度条,但它没有加载任何东西......?

     private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.web_view;
break;
case 1:
resId = R.layout.media;
break;
case 2:
resId = R.layout.youtube;
break;

}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
web = (WebView) findViewById(R.id.web_view);
final ProgressBar bar = (ProgressBar) findViewById(R.id.loading);


web.loadUrl(url);
web.getSettings().setJavaScriptEnabled(true);



web.setWebViewClient(new WebViewClient () {

@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
web.loadUrl("javascript:var element = document.getElementById('social-tabs'); element.parentElement.removeChild(element);");
web.loadUrl("javascript:var element = document.getElementById('omc-top-banner'); element.parentElement.removeChild(element);");
web.loadUrl("javascript:var element = document.getElementById('simplereach- slide-element'); element.parentElement.removeChild(element);");

}

@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);


}

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


}

});

return view;
}

最佳答案

您需要在您的 webview 客户端中实现 onProgressChanged 方法来适本地更新进度条:

      final Activity MyActivity = this;

web.setWebViewClient(new WebViewClient () {

.
.
.
public void onProgressChanged(WebView view, int progress) {

MyActivity.setProgress(progress * 100);

ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar2);

//pb is equivalent to your "bar" progress bar variable

pb.setVisibility(View.VISIBLE);
pb.setProgress(progress);
if (progress == 100) {
pb.setVisibility(View.GONE);



}
}
}

关于android - 带有 WebView 的 ViewPager 中的 ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com