简体   繁体   中英

Android tabs not switching correctly

So I have some simple code inside the getItem method of the FragmentPagerAdapter Class:

@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:{
            return new FragmentOne();
        }
        default:
            return new FragmentTwo();
    }
}

Additional information: getCount returns 4

However, what happens is that the first fragment does display correctly, but the remaining three tabs seem to bug out. When I swipe over once, it displays correctly. I swipe to the right again (so tab #3) and nothing is displayed. Swipe right again (tab #4) nothing. Swipe left (tab #3) displays correctly. Swipe left (tab #2) nothing. Tab #1 always displays correctly. What inside of my fragments could be causing this strange error? It's definitely something with my fragments, I know that :)

EDIT: In hopes to clarify the issue, I try to change the environment a little bit - I did ViewPager.setOffscreenPageLimit(3) in order to load all tabs. I made 4 cases, with each getting a different fragment. What happens now is that the fragment that should be in the last tab is loaded in the first tab, and all of the other tabs are empty.

Thanks in advance!

Try something like this. Write inner class for your activity:

class TabsPagerAdapter extends FragmentStatePagerAdapter {

    public TabsPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
            case 0:
                return firstTabFragment;
            case 1:
                return secondTabFragment;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

}

In your Fragment classes in onCreate method write setRetainInstance(true) .

In your activity in onCreate method register your fragments and tabs like this:

    // for fragments
    FragmentManager fragmentManager = getSupportFragmentManager();

    firstTabFragment = (FirstTabFragment) fragmentManager.findFragmentByTag(TAG_FIRST_FRAGMENT);
    if (firstTabFragment == null) {
        firstTabFragment = new FirstTabFragment();
        fragmentManager.beginTransaction()
                .add(R.id.pager, firstTabFragment, TAG_FIRST_FRAGMENT)
                .commit();
        firstTabFragment.setData(yourDataIfNeeded);
    }

    // for tabs
    String[] tabs = {getString(R.string.first_tab), getString(R.string.second_tab)};
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    TabsPagerAdapter mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (String tabText : tabs) {
        Tab tab = actionBar.newTab();
        tab.setText(tabText);
        tab.setTabListener(this);
        actionBar.addTab(tab);
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

Also add onSaveInstanceState method for your activity like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString(KEY_DATA, yourDataIfNeeded);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM