2016-06-10 7 views
0

내 블로그에 간단한 앱을 끝내고 필요한 것은 화면에로드되는 것을 나타내는 ProgressBar를 내 WebView 액티비티에 추가하는 것입니다. ProgressDialog를 사용하고 싶지 않음)내 WebView 액티비티에 ProgressBar 추가하기

어떻게하면됩니까? (각 링크 클릭도)

미리 감사드립니다 !!

내 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

progressbar와 함께 framelayout에 webview를 추가하고 페이지를로드 할 때 가시성을 변경하십시오. 사용자가 여전히 webview를 터치 할 수 있기 때문에 전체 화면 상대 레이아웃에 진행 막대를 놓습니다.

FrameLayout이

-> 웹보기

-> RelativeLayout의

--> PregressBar 
+0

이 답변이 다운 된 투표를 왜 당신이 진행 막대를 표시 할 수 있습니다 여기에 추가, 이해하지 못하는 부하 URL webview에서 호출되고 onPageFinished에서 숨 깁니다. –

+0

고마워,하지만 ProgressBar가 "사라져서"WebView에서 항상 돌아가고있다. –

+0

내 마지막 클래스 "MyWebViewClient"가 아무 것도하지 않고 삭제하고 앱이 정상적으로 작동하지만, 진행 막대 넣기 ... :( –