如何使WebView快速加载并向其中添加进度栏 [英] How to make a WebView Load fast And adding a Progress bar to it

查看:105
本文介绍了如何使WebView快速加载并向其中添加进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的应用程序,其中放置了Web视图,但是页面加载非常慢(许多网站都尝试过),并且我还想添加进度条,以便它应显示页面正在加载. 这是我用于webview的代码,但我不知道如何获取进度条:

I am making a simple application in which I have put a webview, but the page is loading very slow(tried with many websites) and also I want to add a progress bar so it should show that the page is loading. Here is the code I am using for webview but I have no idea how to get progress bar on it:

public class MainActivity extends Activity {

WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv = (WebView) findViewById(R.id.webView1);
    wv.setWebViewClient(new webCont());

    wv.loadUrl(" http://www.abc.com" ); 
}
 class webCont extends WebViewClient
{

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);

        return true;
    }
}

推荐答案

具有进度栏.显示相同.您可以根据需要自定义以下内容.要加载得更快,取决于连接速度.

Have a progress bar. Display the same. You customize the below to suit your needs. To load faster it depends on the connection speed.

覆盖onPageFinished并将可见性设置为在Webcount中消失

Override onPageFinished and set the visibility to gone in webcount

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web = (WebView) findViewById(R.id.wv);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://slashdot.org/");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // TODO Auto-generated method stub
             Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            //super.onReceivedError(view, errorCode, description, failingUrl);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        else
        {
            finish();
            return true;
        }
    }
}

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button1"
        android:id="@+id/wv"/>
    <ProgressBar
       android:id="@+id/progressBar"
       android:layout_width="wrap_content"
       android:layout_centerInParent="true"
       android:layout_height="wrap_content"
       />


</RelativeLayout>

捕捉

如果不滚动,您将看不到整个页面

Without scrolling you don't see entire page

向右滚动

这篇关于如何使WebView快速加载并向其中添加进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆