Created
April 4, 2017 10:14
-
-
Save hardysim/53edbae66d8046b7cd7648061c5f1435 to your computer and use it in GitHub Desktop.
Like a normal FragmentPagerAdapter but holds a list of instantiated fragments and the current primary 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
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.util.SparseArray; | |
import android.view.ViewGroup; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Like a normal {@link FragmentPagerAdapter} but holds a list of instantiated fragments | |
* which can be queried using {@link #getInstantiatedFragments(int)}. | |
* You can get the primary fragment using {@link #getPrimaryFragment()}. | |
* <p> | |
* When you want to know, when the ViewPager-page changes, subscribe as a {@link PrimaryFragmentListener} | |
* instead of {@link ViewPager.OnPageChangeListener} because the latter will be called | |
* <strong>before</strong> the page is actually instantiated. | |
* | |
* @see <a href="http://stackoverflow.com/a/15261142/2170109">http://stackoverflow.com/a/15261142/2170109</a> | |
*/ | |
public abstract class SmartFragmentPagerAdapter extends FragmentPagerAdapter { | |
private final SparseArray<Fragment> instantiatedFragments = new SparseArray<>(); | |
private List<PrimaryFragmentListener> primaryFragmentListeners; | |
@Nullable private Fragment primaryFragment; | |
protected SmartFragmentPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
// region PagerAdapter | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
Fragment fragment = (Fragment) super.instantiateItem(container, position); | |
instantiatedFragments.put(position, fragment); | |
return fragment; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
instantiatedFragments.remove(position); | |
super.destroyItem(container, position, object); | |
} | |
@Override | |
public void setPrimaryItem(ViewGroup container, int position, Object object) { | |
super.setPrimaryItem(container, position, object); | |
primaryFragment = (Fragment) object; | |
dispatchSetPrimaryItem(position, object); | |
} | |
// endregion | |
// region PrimaryFragmentListener | |
public interface PrimaryFragmentListener { | |
void onPrimaryFragmentChanged(int position, Fragment fragment); | |
} | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
public void addPrimaryFragmentListener(PrimaryFragmentListener listener) { | |
if (primaryFragmentListeners == null) { | |
primaryFragmentListeners = new ArrayList<>(); | |
} | |
primaryFragmentListeners.add(listener); | |
} | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
public void removePrimaryFragmentListener(PrimaryFragmentListener listener) { | |
if (primaryFragmentListeners != null) { | |
primaryFragmentListeners.remove(listener); | |
} | |
} | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
public void clearPrimaryFragmentListener() { | |
if (primaryFragmentListeners != null) { | |
primaryFragmentListeners.clear(); | |
} | |
} | |
// endregion | |
private void dispatchSetPrimaryItem(int position, Object object) { | |
if (primaryFragmentListeners != null) { | |
for (int i = 0; i < primaryFragmentListeners.size(); i++) { | |
PrimaryFragmentListener listener = primaryFragmentListeners.get(i); | |
if (listener != null) { | |
listener.onPrimaryFragmentChanged(position, (Fragment) object); | |
} | |
} | |
} | |
} | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
public Fragment getInstantiatedFragments(int position) { | |
return instantiatedFragments.get(position); | |
} | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
@Nullable | |
public Fragment getPrimaryFragment() { | |
return primaryFragment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment