wawanopoulos Posté(e) 26 août 2012 Share Posté(e) 26 août 2012 Bonjour à tous, Depuis quelques jours, je me suis mis au développement d'une application de type [swype+Tab] en utilisant la nouveauté du SDK Android qui permet de sélectionner dès la création du projet ce type d'application. Cela génère un bon morceau de code qui nous facilite bien la vie. J'ai donc une application avec 3 onglets qui contiennent 3 fragments. A chacun d'entre eux est associé une classe dans laquelle je crée mon layout et dans chacun de ces layouts se trouve une WebView. Je souhaiterais pouvoir afficher une progressBar dans chacun de ces fragments tant que la page web de la WebView n'est pas chargée. Je me pose deux questions : - J'ai essayé d'intégrer une ProgressDialog "normale" mais des méthodes comme le getWindow ne sont pas reconnues car je suis dans une classe qui hérite de fragment et non d'une Activity. Es-ce un bon choix? Comment faut-il faire sinon pour afficher une classe de type Activity dans une application Swype+Tab ? - Existe-t-il un moyen d'afficher une ProgressBar dans un fragment ? Voici le code en commençant par la mainActivity : import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.NavUtils; import android.support.v4.view.ViewPager; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by the adapter. // Also specify this Activity object, which implements the TabListener interface, as the // listener for when this tab is selected. actionBar.addTab( actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { Fragment fragment = null; switch(i){ case 0: fragment = new CreerFragment(); break; case 1: fragment = new RepondreFragment(); break; case 2: fragment = new StatsFragment(); break; default: } //set args if necessary Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); fragment.setArguments(args); //return fragment return fragment; } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(); case 1: return getString(R.string.title_section2).toUpperCase(); case 2: return getString(R.string.title_section3).toUpperCase(); } return null; } } /** * A dummy fragment representing a section of the app, but that simply displays dummy text. */ public static class DummySectionFragment extends Fragment { public DummySectionFragment() { } public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER); Bundle args = getArguments(); textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); return textView; } } } Voici le code d'exemple d'une classe (CreerFragment) avec une WebView : (je ne mets pas les 3 c'est la même chose, c'est juste l'adresse qui change) : package com.example.idea; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebSettings; import android.webkit.WebView; public class CreerFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.repondrefragment, null); WebView wv = (WebView) v.findViewById(R.id.webView1); WebSettings webSettings = wv.getSettings(); webSettings.setSavePassword(true); webSettings.setSaveFormData(true); webSettings.setJavascriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); webSettings.setSupportZoom(false); wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); wv.setScrollbarFadingEnabled(false); wv.loadUrl("http://monsite.com"); return v; } } Et le layout associé à cette classe : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> Quelqu'un saurait-il m'indiquer comment faire ? Merci d'avance pour votre aide. Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
chpil Posté(e) 27 août 2012 Share Posté(e) 27 août 2012 Une ProgressBar, c'est un widget comme un autre. A ce titre, tu peux l'inclure dans le layout de ton Fragment Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
Recommended Posts
Rejoignez la conversation
Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.