2016-06-10 30 views
0

即時完成我的博客的簡單應用程序,我需要做的唯一事情就是將ProgressBar添加到我的WebView活動中,以指示屏幕上正在加載某些內容(我不' t想要使用ProgressDialog)將ProgressBar添加到我的WebView活動中

我該怎麼做? (併爲每個鏈接點擊)

在此先感謝!

我的MainActivity

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" /> 

回答

0

將webview與進度條一起放入framelayout中,並在加載頁面時更改其可見性。由於用戶仍然可以觸摸webview你cqn把進度條放在全屏相對佈局。

的FrameLayout

- >網頁視圖

- > RelativeLayout的

--> PregressBar 
+0

我不明白爲什麼這個答案下降投票,添加到這一點,你可以顯示進度條,當負載網址在webview上調用並將其隱藏 -​​ onPageFinished。 –

+0

謝謝,但現在ProgressBar不能「消失」,並一直在WebView上旋轉 –

+0

發現我的最終課程「MyWebViewClient」沒有做任何事情,刪除它,應用程序正常工作,但我仍然可以'把一個進度條... :( –