summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/AppCompat/FormsFragmentPagerAdapter.cs
blob: 99fbb396b4f6de737aafad95037734fb1614e721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
´╗┐using Android.OS;
using Android.Support.V4.App;
using Java.Lang;

namespace Xamarin.Forms.Platform.Android.AppCompat
{
	internal class FormsFragmentPagerAdapter<T> : FragmentPagerAdapter where T : Page
	{
		MultiPage<T> _page;

		public FormsFragmentPagerAdapter(MultiPage<T> page, FragmentManager fragmentManager) : base(fragmentManager)
		{
			_page = page;
		}

		public override int Count => CountOverride;

		public int CountOverride { get; set; }

		public override Fragment GetItem(int position)
		{
			return FragmentContainer.CreateInstance(_page.Children[position]);
		}

		public override long GetItemId(int position)
		{
			return _page.Children[position].GetHashCode();
		}

		public override int GetItemPosition(Object objectValue)
		{
			var fragContainer = objectValue as FragmentContainer;
			if (fragContainer != null && fragContainer.Page != null)
			{
				int index = _page.Children.IndexOf(fragContainer.Page);
				if (index >= 0)
					return index;
			}
			return PositionNone;
		}

		public override ICharSequence GetPageTitleFormatted(int position)
		{
			return new String(_page.Children[position].Title);
		}

		// http://stackoverflow.com/questions/18642890/fragmentstatepageradapter-with-childfragmentmanager-fragmentmanagerimpl-getfra/19099987#19099987
		public override void RestoreState(IParcelable state, ClassLoader loader)
		{
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
				_page = null;
			base.Dispose(disposing);
		}
	}
}