Created
January 22, 2015 11:09
-
-
Save geirsagberg/7c65ab9cdd03ddec1fba to your computer and use it in GitHub Desktop.
MvxViewPagerFragmentAdapter
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Android.App; | |
using Android.Content; | |
using Android.Support.V13.App; | |
using Cirrious.MvvmCross.Droid.FullFragging.Fragments; | |
using Cirrious.MvvmCross.ViewModels; | |
using Java.Lang; | |
using String = Java.Lang.String; | |
public class MvxViewPagerFragmentAdapter | |
: FragmentPagerAdapter | |
{ | |
private readonly Context _context; | |
public IEnumerable<FragmentInfo> Fragments { get; private set; } | |
public override int Count | |
{ | |
get { return Fragments.Count(); } | |
} | |
public MvxViewPagerFragmentAdapter( | |
Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments) | |
: base(fragmentManager) | |
{ | |
_context = context; | |
Fragments = fragments; | |
} | |
public override Fragment GetItem(int position) | |
{ | |
var fragmentInfo = Fragments.ElementAt(position); | |
var fragment = Fragment.Instantiate(_context, | |
FragmentJavaName(fragmentInfo.FragmentType)); | |
((MvxFragment) fragment).ViewModel = fragmentInfo.ViewModel; | |
return fragment; | |
} | |
protected static string FragmentJavaName(Type fragmentType) | |
{ | |
var namespaceText = fragmentType.Namespace ?? ""; | |
if (namespaceText.Length > 0) | |
namespaceText = namespaceText.ToLowerInvariant() + "."; | |
return namespaceText + fragmentType.Name; | |
} | |
public override ICharSequence GetPageTitleFormatted(int position) | |
{ | |
return new String(Fragments.ElementAt(position).Title); | |
} | |
public class FragmentInfo | |
{ | |
public string Title { get; set; } | |
public Type FragmentType { get; set; } | |
public IMvxViewModel ViewModel { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment