Created
October 29, 2015 09:08
-
-
Save Krumelur/02ee7dc1349ca848d0ad to your computer and use it in GitHub Desktop.
Generic pager adapter for use with Android PagerTabStrip. This one used the v13 version with "native" Fragments
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 GenericPagerAdpapter<T> : Android.Support.V13.App.FragmentPagerAdapter where T : Fragment | |
{ | |
public GenericPagerAdpapter (FragmentManager fm) : base(fm) | |
{ | |
} | |
public Func<int, T> ItemAtPosition; | |
public Func<int> ItemCount; | |
public Func<int, string> TitleAtPosition; | |
public override int Count | |
{ | |
get { | |
return this.ItemCount(); | |
} | |
} | |
public override Fragment GetItem (int position) | |
{ | |
return this.ItemAtPosition(position); | |
} | |
public override ICharSequence GetPageTitleFormatted(int position) | |
{ | |
var stringTitle = this.TitleAtPosition(position); | |
var titles = CharSequence.ArrayFromStringArray(new [] { stringTitle }); | |
return titles[0]; | |
} | |
} |
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
var fragments = new Fragment[] { | |
new TimeFragment (), | |
new StopwatchFragment (), | |
new AboutFragment () | |
}; | |
var titles = new [] { | |
"Time", | |
"Stopwatch", | |
"About" | |
}; | |
var viewPager = FindViewById<Android.Support.V4.View.ViewPager> (Resource.Id.viewPager); | |
viewPager.Adapter = new GenericPagerAdpapter<Fragment> (this.FragmentManager) { | |
ItemCount = () => titles.Length, | |
ItemAtPosition = pos => fragments [pos], | |
TitleAtPosition = pos => titles [pos] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment