Last active
August 29, 2015 14:27
-
-
Save Sirelon/5ed229bf8a900d91edd5 to your computer and use it in GitHub Desktop.
Simple fragment adapter for ViewPager.
This file contains hidden or 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
package com.sirelon; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentStatePagerAdapter; | |
import java.util.List; | |
/** | |
* @author romanishin | |
* @since 18.08.15. | |
*/ | |
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter { | |
private final List<Fragment> mFragments; | |
private final List<CharSequence> mTitles; | |
public ViewPagerFragmentAdapter(FragmentManager fm, List<Fragment> fragments) { | |
this(fm, fragments, null); | |
} | |
public ViewPagerFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<CharSequence> titles) { | |
super(fm); | |
mFragments = fragments; | |
mTitles = titles; | |
if (mTitles != null) | |
if (mFragments.size() != mTitles.size()) | |
throw new IllegalArgumentException("Size of fragments and title's size not equal!"); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return mFragments.get(position); | |
} | |
@Override | |
public int getCount() { | |
return mFragments.size(); | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
if (mTitles != null) | |
return mTitles.get(position); | |
return super.getPageTitle(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment