Last active
December 19, 2015 18:29
-
-
Save Cheesebaron/5999592 to your computer and use it in GitHub Desktop.
A couple of code snippets for a blog post about Fragments, ViewPager and MvvmCross.
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<dk.ostebaronen.droid.viewpagerindicator.TitlePageIndicator | |
android:id="@+id/viewPagerIndicator" | |
android:padding="10dip" | |
android:layout_height="wrap_content" | |
android:layout_width="fill_parent" | |
app:footerLineHeight="1dp" | |
app:footerIndicatorHeight="3dp" | |
app:footerIndicatorStyle="underline" | |
app:selectedBold="true" | |
/> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/viewPager" | |
android:layout_width="fill_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" /> | |
</LinearLayout> |
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.Collections.Generic; | |
using Android.App; | |
using Android.Support.V4.View; | |
using Cirrious.MvvmCross.Droid.Fragging; | |
using Cirrious.MvvmCross.Droid.Views; | |
using DK.Ostebaronen.Droid.ViewPagerIndicator; | |
using CoolProject.Core.ViewModels; | |
using CoolProject.Droid.Adapters; | |
using CoolProject.Droid.Views.Fragments; | |
namespace CoolProject.Droid.Views | |
{ | |
[Activity(Label = "Home")] | |
public class HomeView : MvxFragmentActivity | |
{ | |
private ViewPager _viewPager; | |
private TitlePageIndicator _pageIndicator; | |
private MvxViewPagerFragmentAdapter _adapter; | |
public new HomeViewModel ViewModel | |
{ | |
get { return (HomeViewModel)base.ViewModel; } | |
set { base.ViewModel = value; } | |
} | |
protected override void OnCreate(Android.OS.Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
SetContentView(Resource.Layout.Home); | |
var fragments = new List<MvxViewPagerFragmentAdapter.FragmentInfo> | |
{ | |
new MvxViewPagerFragmentAdapter.FragmentInfo | |
{ | |
FragmentType = typeof(FirstFragment), | |
Title = "Fragment1", | |
ViewModel = ViewModel.First | |
}, | |
new MvxViewPagerFragmentAdapter.FragmentInfo | |
{ | |
FragmentType = typeof(SecondFragment), | |
Title = "Fragment2", | |
ViewModel = ViewModel.Second | |
}, | |
new MvxViewPagerFragmentAdapter.FragmentInfo | |
{ | |
FragmentType = typeof(ThirdFragment), | |
Title = "Fragment3", | |
ViewModel = ViewModel.Third | |
} | |
}; | |
_viewPager = FindViewById<ViewPager>(Resource.Id.viewPager); | |
_adapter = new MvxViewPagerFragmentAdapter(this, SupportFragmentManager, fragments); | |
_viewPager.Adapter = _adapter; | |
_pageIndicator = FindViewById<TitlePageIndicator>(Resource.Id.viewPagerIndicator); | |
_pageIndicator.SetViewPager(_viewPager); | |
_pageIndicator.CurrentItem = 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
using System.Windows.Input; | |
using Cirrious.MvvmCross.Plugins.Location; | |
using Cirrious.MvvmCross.Plugins.PictureChooser; | |
using Cirrious.MvvmCross.ViewModels; | |
using CoolProject.Core.Services; | |
namespace CoolProject.Core.ViewModels | |
{ | |
public class HomeViewModel | |
: MvxViewModel | |
{ | |
private readonly ICoolService _service; | |
public HomeViewModel(ICoolService service, IMvxGeoLocationWatcher geoLocationWatcher, IMvxPictureChooserTask pictureChooserTask) | |
{ | |
_service = service; | |
First = new FirstViewModel(service, geoLocationWatcher); | |
Second = new SecondViewModel(service); | |
Third = new ThirdViewModel(service, pictureChooserTask); | |
} | |
... | |
} | |
} |
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.Content; | |
using Android.Support.V4.App; | |
using Cirrious.MvvmCross.Droid.Fragging.Fragments; | |
using Cirrious.MvvmCross.ViewModels; | |
namespace CoolProject.Droid.Adapters | |
{ | |
public class MvxViewPagerFragmentAdapter | |
: FragmentPagerAdapter | |
{ | |
public class FragmentInfo | |
{ | |
public string Title { get; set; } | |
public Type FragmentType { get; set; } | |
public IMvxViewModel ViewModel { get; set; } | |
} | |
private readonly Context _context; | |
public MvxViewPagerFragmentAdapter( | |
Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments) | |
: base(fragmentManager) | |
{ | |
_context = context; | |
Fragments = fragments; | |
} | |
public IEnumerable<FragmentInfo> Fragments { get; private set; } | |
public override int Count | |
{ | |
get { return Fragments.Count(); } | |
} | |
public override Fragment GetItem(int position) | |
{ | |
var frag = Fragments.ElementAt(position); | |
var fragment = Fragment.Instantiate(_context, | |
FragmentJavaName(frag.FragmentType)); | |
((MvxFragment)fragment).DataContext = frag.ViewModel; | |
return fragment; | |
} | |
protected virtual string FragmentJavaName(Type fragmentType) | |
{ | |
var namespaceText = fragmentType.Namespace ?? ""; | |
if (namespaceText.Length > 0) | |
namespaceText = namespaceText.ToLowerInvariant() + "."; | |
return namespaceText + fragmentType.Name; | |
} | |
public override Java.Lang.ICharSequence GetPageTitleFormatted(int p0) { return new Java.Lang.String(Fragments.ElementAt(p0).Title); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment