Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Created April 2, 2016 04:30
Show Gist options
  • Save patrickhammond/7181750a76341d71b5c957881f60d8e9 to your computer and use it in GitHub Desktop.
Save patrickhammond/7181750a76341d71b5c957881f60d8e9 to your computer and use it in GitHub Desktop.
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