This repository was archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathIconTabbedPageRenderer.cs
75 lines (65 loc) · 2.36 KB
/
IconTabbedPageRenderer.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Support.Design.Widget;
using FormsPlugin.Iconize;
using FormsPlugin.Iconize.Droid;
using Plugin.Iconize.Droid.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(IconTabbedPage), typeof(IconTabbedPageRenderer))]
namespace FormsPlugin.Iconize.Droid
{
/// <summary>
/// Defines the <see cref="IconTabbedPageRenderer" /> renderer.
/// </summary>
/// <seealso cref="Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer" />
public class IconTabbedPageRenderer : TabbedPageRenderer
{
private readonly List<String> _icons = new List<String>();
/// <summary>
/// Called when [attached to window].
/// </summary>
protected override void OnAttachedToWindow()
{
UpdateTabbedIcons(Context);
base.OnAttachedToWindow();
}
/// <summary>
/// Raises the <see cref="E:ElementChanged" /> event.
/// </summary>
/// <param name="e">The <see cref="ElementChangedEventArgs{TabbedPage}" /> instance containing the event data.</param>
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
_icons.Clear();
if (e.NewElement != null)
{
foreach (var page in e.NewElement.Children)
{
_icons.Add(page.Icon.File);
page.Icon = null;
}
}
base.OnElementChanged(e);
}
/// <summary>
/// Updates the tabbed icons.
/// </summary>
private void UpdateTabbedIcons(Context context)
{
var tabLayout = FindViewById<TabLayout>(IconControls.TabLayoutId);
if (tabLayout == null || tabLayout.TabCount == 0)
return;
for (var i = 0; i < tabLayout.TabCount; i++)
{
var tab = tabLayout.GetTabAt(i);
var icon = Plugin.Iconize.Iconize.FindIconForKey(_icons[i]);
if (icon == null)
continue;
var drawable = new IconDrawable(context, icon).Color(Color.White.ToAndroid()).SizeDp(20);
tab.SetIcon(drawable);
}
}
}
}