2012-04-23 2 views
5

Ho un'attività Fragment che ha un ListFragment sulla sinistra e un Fragment sulla destra che ha una WebView. La funzionalità funziona bene, ma mi piacerebbe visualizzare una finestra di dialogo di avanzamento che mostra "Caricamento in corso ..." mentre la pagina web termina il caricamento. Come posso realizzare questo compito apparentemente banale?Come faccio a visualizzare ProgressDialog usando Fragment

Qui di seguito è il mio codice:

public class ArticleListActivity extends FragmentActivity implements 
    ArticleListFragment.OnArticleSelectedListener { 

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

    public void onArticleSelected(String contentLink) { 
     ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.articleview_fragment); 

     if (viewer == null || !viewer.isInLayout()) { 
      Intent showContent = new Intent(getApplicationContext(), 
        ArticleViewerActivity.class); 

      showContent.setData(Uri.parse(contentLink)); 
      startActivity(showContent); 
     } else { 
      viewer.updateUrl(contentLink); 
     } 
    } 
} 

Ecco il mio FragmentActivity

public class ArticleViewerActivity extends FragmentActivity { 

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

     Intent launchingIntent = getIntent(); 
     String content = launchingIntent.getData().toString(); 

     ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.articleview_fragment); 

     viewer.updateUrl(content); 
    } 
} 

e qui è la mia Frammento

public class ArticleViewerFragment extends Fragment { 

    private ProgressDialog progressBar; 

    private WebView viewer = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     viewer = (WebView) inflater.inflate(R.layout.webview_layout, container, false); 
     viewer.loadUrl("file:///android_asset/default.html"); 
     return viewer; 
    } 

    public void updateUrl(String newUrl) { 
     if (viewer != null) { 
      viewer.loadUrl(newUrl); 
     } 
    } 
} 

risposta

1

di questo codice:

final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true); 
pd.setCancelable(false); 

webview.getSettings().setJavaScriptEnabled(true); 
webview.getSettings().setSupportZoom(true); 
webview.getSettings().setBuiltInZoomControls(true); 
webview.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onPageFinished(WebView view, String url) { 
     if (pd != null && pd.isShowing()) { 
      pd.dismiss(); 
     } 
    } 
}); 
webview.loadUrl("file:///android_asset/default.html");