Created
April 2, 2016 04:30
-
-
Save patrickhammond/7181750a76341d71b5c957881f60d8e9 to your computer and use it in GitHub Desktop.
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
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.util.SparseArrayCompat; | |
import android.view.ViewGroup; | |
public abstract class IndexedFragmentPagerAdapter<T> extends FragmentPagerAdapter { | |
private SparseArrayCompat<T> fragments = new SparseArrayCompat<>(); | |
public IndexedFragmentPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
@SuppressWarnings("unchecked") | |
T fragment = (T) super.instantiateItem(container, position); | |
fragments.put(position, fragment); | |
return fragment; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
fragments.remove(position); | |
super.destroyItem(container, position, object); | |
} | |
public T getFragmentAtPosition(int position) { | |
return fragments.get(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment