简体   繁体   中英

I've got a weird bug when using a ViewPager inside a Fragment

Okay i'll try and make this as clear as possible. I have a Fragment called CheckerManager which contains a ViewPager. This ViewPager will allow the user to swipe between 3 Fragments all of which are an instance of another Fragment called CheckerFragment. I'm using a FragmentPagerAdapter to handle paging. Here's how it looks

private class PagerAdapter extends FragmentPagerAdapter {
    CharSequence mTabTitles[];

    public PagerAdapter(FragmentManager fm, CharSequence tabTitles[]) {
        super(fm);
        mTabTitles = tabTitles;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_LOTTO);
            case 1:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_DAILY);
            case 2:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_EURO);
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabTitles[position];
    }

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

I know that the ViewPager will always create the Fragment either side of the current Fragment. So say my 3 CheckerFragments are called A, B and C and the current Fragment is A. B has already been created. But my problem is that even though I am still looking at Fragment A, Fragment B is the 'active' Fragment. Every input I make is actually corresponding to Fragment B and not A. The active Fragment is always the one which has been created last by the ViewPager.

I've looked at quite a few things to see if anyone has had the same problem but i'm finding it difficult to even describe what's wrong. I think it's something to with the fact that all of the ViewPagers fragments are of the same type ie - CheckerFragment. I have a working implementation of a ViewPager inside a fragment elsewhere in the application and the only difference I can tell is that each page is a different type of Fragment.

Any help would be appreciated!

*EDIT

PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), tabTitles);
    ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewPagerChecker);
    viewPager.setAdapter(adapter);

I feel pretty stupid but I found out what the issue was. In my CheckerFragment I would call getArguments() to retrieve a String extra and I would use this to determine how to layout the fragment. Problem was I made this extra a static member of CheckerFragment. So every time a new Fragment was created it was using the most recent extra.

Moral of the story - Don't make your fragments extra a static member if you plan on making multiple instances of that fragment.

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