FragmentPagerAdapter getItem wrong position
Asked Answered
C

4

8

I've got strange problem with 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;
    }
    /*
     * Title
     */
    @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) return wrong position when i try to switch between 3 tabs. When i create app with 2 tabs only, everything work just fine. Adding more then 2 of them, creating strange problem. Switch from 0 to 1 position - works fine, switch from 1 to 0 - works fine, switch from 1 to 2 posiition - works fine, but, when i try to go back from 2 to 1 position, public Fragment getItem(int position) - int position return "0" instead of "1". Does anyone help me with this ?

Corbin answered 30/6, 2013 at 14:36 Comment(1)
This seems like critical issue, any one has "correct solution" for this?Willette
C
9

Ok, I've found the solution. First of all, the getItem "int position", doesn't indicate current display fragment. To display 3 or more tabs, without unload firts fragment You must added this line:

mViewPager.setOffscreenPageLimit(3);

End of story...

Corbin answered 1/7, 2013 at 6:50 Comment(2)
this is a very bad advice.. you must not keep your fragments alive when they are not in use, so that's y android will call to finish it. And by adding this line mViewPager.setOffscreenPageLimit(count); your asking android not to clear these unused fragment, think what happens when your fragment has loads of images..Cleistogamy
@Corbin did you resolve the problem i am facing the same problem public Fragment getItem(int i) call twice which give the wrong position i have 5 tabs with 5 fragments if(i>0){ i = i-1; } work but the problem is that last tab or fragment not been calling b/c position = position -1 mViewPager.setOffscreenPageLimit(4); not working for meWatercourse
I
3

You just need:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.d("test", "position = " + position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

The position in onPageSelected is what you want.

Identic answered 22/4, 2015 at 5:24 Comment(0)
E
2

I had had the same problem, and I used fragment inside each tabs; So instead of

mViewPager.setAdapter(new MainTabs(getFragmentManager()));

use this one:

mViewPager.setAdapter(new MainTabs(getChildFragmentManager()));
Eudemonia answered 17/7, 2015 at 2:50 Comment(2)
Anyway to use getChildFragmentManager() using minSDK 14?Pommel
Cannot get getChildFragmentManager in support (Which is needed unless you're bleeding edge..)Varityper
L
1

In case anyone else has this issue, I solved it by not using the position given. Instead, I get a List of Fragment with getSupportFragmentManager, loop through, and check if the current Fragment is instanceof my desired Fragment.

Luik answered 23/7, 2017 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.