I have a strange problem with the FramentPageAdapter
MainActivity.java
@SuppressLint("ValidFragment") public class MainActivity<DashboardActivity> extends FragmentActivity implements ActionBar.TabListener { ... ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(0)).setTabListener(this).setIcon(R.drawable.rating_good)); actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(1)).setTabListener(this).setIcon(R.drawable.action_search)); actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(2)).setTabListener(this).setIcon(R.drawable.action_search)); } ... public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } public Fragment getItem(int position) { Fragment fragment = null; switch(position) { case 0: fragment = new Fragment0(); break; case 1: fragment = new Fragment1(); break; case 2: fragment = new Fragment2(); break; } return fragment; } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section0).toUpperCase(l); case 1: return getString(R.string.title_section1).toUpperCase(l); case 2: return getString(R.string.title_section2).toUpperCase(l); } return null; } }
public Fragment getItem (int position) returns the wrong position when I try to switch between 3 tabs. When I create an application with only 2 tabs, everything works fine. Adding more than two of them, creating a strange problem. Switching from 0 to 1 position - working fine, switching from 1 to 0 - working fine, switching from 1 to 2 positioning - working fine, but when I try to return from 2 to 1 position, public Fragment getItem (int position) - int position returns "0" instead of "1". Does anyone help me with this?
source share