-
-
Save NLMartian/6793042 to your computer and use it in GitHub Desktop.
从ViewPager遍历Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { | |
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>(); | |
private ScreenSlidePagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public android.support.v4.app.Fragment getItem(int i) { | |
switch (i) { | |
case 0: | |
return TodayFragment.newInstance(0); | |
case 1: | |
return TodayFragment.newInstance(1); | |
case 2: | |
return TodayFragment.newInstance(2); | |
default: | |
return null; | |
} | |
} | |
@Override | |
public int getCount() { | |
return NUM_SLIDES; | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
Fragment fragment = (Fragment) super.instantiateItem(container, position); | |
registeredFragments.put(position, fragment); | |
return fragment; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
registeredFragments.remove(position); | |
super.destroyItem(container, position, object); | |
} | |
public Fragment getFragmentAt(int position) { | |
return registeredFragments.get(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
获取ViewPager指定位置Fragment