将ProgressBar添加到我的WebView活动中

时间:2016-06-10 03:45:48

标签: android webview android-progressbar

我为我的博客完成了我的简单应用程序,我唯一需要做的就是在我的WebView Activity中添加一个ProgressBar,以表明屏幕上有东西加载(我不想使用ProgressDialog)

我该怎么做? (并且每个链接也点击)

提前致谢!!

我的主要活动

package com.lfcchile;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private WebView myWebView;

    //Función que almacena la URL actual para compartirla
    private void shareURL() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, myWebView.getUrl());
        startActivity(Intent.createChooser(shareIntent, "Comparte este enlace!"));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareURL();

            }
        });

        this.myWebView = (WebView) this.findViewById(R.id.webViewInicio);
        WebView myWebView = (WebView) this.findViewById(R.id.webViewInicio);

        // Activar JavaScript
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Provide a WebViewClient for your WebView
        myWebView.setWebViewClient(new WebViewClient());

        myWebView.loadUrl("http://lfcchile.com/");

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if (this.myWebView.canGoBack())
                this.myWebView.goBack();
            else
                super.onBackPressed();
        }
    }

    //Funciones que permiten iconos en la Action Bar del MainActivity

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_salir) {
            finish();
            Toast.makeText(getApplicationContext(), "Nos vemos pronto! YNWA!",
                    Toast.LENGTH_LONG).show();
            //shareURL();
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Acciones del menú.
        int id = item.getItemId();

        if (id == R.id.nav_inicio) {
            setTitle(R.string.inicio);
            myWebView.loadUrl("http://lfcchile.com/");
        } else if (id == R.id.nav_destacado) {
            setTitle(R.string.destacado);
            myWebView.loadUrl("http://lfcchile.com/category/destacado/");
        } else if (id == R.id.nav_tabla_pl) {
            setTitle(R.string.tabla_pl);
            myWebView.loadUrl("http://lfcchile.com/tabla-pl/");
        } else if (id == R.id.nav_comunidad) {
            setTitle(R.string.comunidad);
            myWebView.loadUrl("http://lfcchile.com/comunidad/");
        } else if (id == R.id.nav_hillsborough){
            setTitle(R.string.hillsborough);
            myWebView.loadUrl("http://lfcchile.com/hillsborough/");
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if ((Uri.parse(url).getHost().equals("lfcchile.com")) || (Uri.parse(url).getHost().equals("www.facebook.com")) ) {
                // Sitio web a cargar
                return false;
            }

            // Otherwise, the link is not for a page on my site, so launch
            // another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.lfcchile.MainActivity"
tools:showIn="@layout/app_bar_main">

<WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webViewInicio"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

1 个答案:

答案 0 :(得分:0)

将webview放入framelayout以及进度条,并在加载页面时更改其可见性。由于用户仍然可以触摸webview,因此cqn将进度条置于全屏相对布局中。

的FrameLayout

- &GT;网页视图

- &GT; RelativeLayout的

--> PregressBar