Comment définir un onglet par défaut lors de la création de FragmentActivity? J'ai 5 onglets et je veux qu'il s'ouvre sur l'onglet 2, pas 1. Il semble que cela devrait être beaucoup plus facile qu'il ne l'est!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0: {
f = new MasterFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
// case 1 - 3 edited out; i'd like default view "case 1".
default:
throw new IllegalArgumentException("not this many fragments: "
+ position);
}
return f;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.mastercattab1).toUpperCase();
// case 1- 3 edited out
}
return null;
}
}
Et mon XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical" >
<TabHost
Android:id="@Android:id/tabhost"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<LinearLayout
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical" >
<TabWidget
Android:id="@Android:id/tabs"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:layout_weight="0"
Android:orientation="horizontal" />
<FrameLayout
Android:id="@Android:id/tabcontent"
Android:layout_width="0dp"
Android:layout_height="0dp"
Android:layout_weight="0" />
<Android.support.v4.view.ViewPager
Android:id="@+id/viewpager"
Android:layout_width="fill_parent"
Android:layout_height="0dp"
Android:layout_weight="1" />
</LinearLayout>
</TabHost>
</LinearLayout>
Avez-vous essayé d'appeler mViewPager.setCurrentItem(tab.getPosition());
à la fin de onCreate()
?