Created
February 13, 2017 15:45
-
-
Save rotorgames/5b934f847f209e4933ed23dd282c3279 to your computer and use it in GitHub Desktop.
Left icon, title align, droid
This file contains hidden or 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.ComponentModel; | |
| using System.Linq; | |
| using System.Reflection; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using Android.App; | |
| using Android.Content; | |
| using Android.Graphics; | |
| using Android.OS; | |
| using Android.Runtime; | |
| using Android.Views; | |
| using Android.Widget; | |
| using Rg.Forms.Droid.Helpers; | |
| using Rg.Forms.Droid.Renderers.Pages; | |
| using WeeklyBalance.Droid.Renderers.Base; | |
| using WeeklyBalance.Views.Base; | |
| using Xamarin.Forms; | |
| using Xamarin.Forms.Platform.Android; | |
| using TextAlignment = Android.Views.TextAlignment; | |
| using Toolbar = Android.Support.V7.Widget.Toolbar; | |
| using View = Android.Views.View; | |
| [assembly: ExportRenderer(typeof(WbContentPage), typeof(WbContentPageRenderer))] | |
| namespace WeeklyBalance.Droid.Renderers.Base | |
| { | |
| class WbContentPageRenderer : RgContentPageRenderer | |
| { | |
| private const int ToolbarLeftOffset = 16; | |
| private ImageButton _navigationButton; | |
| private Toolbar _toolbar | |
| { | |
| get | |
| { | |
| return RootView.FindViewById(Resource.Id.toolbar) as Toolbar; | |
| } | |
| } | |
| private WbContentPage _element | |
| { | |
| get { return (WbContentPage) Element; } | |
| } | |
| private NavigationPage _navPage | |
| { | |
| get { return Xamarin.Forms.Application.Current.MainPage as NavigationPage; } | |
| } | |
| private ToolbarItem _leftButton; | |
| protected override void OnElementChanged(ElementChangedEventArgs<Page> e) | |
| { | |
| base.OnElementChanged(e); | |
| var navPage = Xamarin.Forms.Application.Current.MainPage as NavigationPage; | |
| if (navPage != null && e.NewElement != null) | |
| { | |
| navPage.Popped += OnPageChanged; | |
| navPage.PoppedToRoot += OnPageChanged; | |
| } | |
| if (navPage != null && e.OldElement != null) | |
| { | |
| OnDisposed(); | |
| } | |
| } | |
| protected override void Dispose(bool disposing) | |
| { | |
| base.Dispose(disposing); | |
| OnDisposed(); | |
| } | |
| protected override void DispatchDraw(Canvas canvas) | |
| { | |
| base.DispatchDraw(canvas); | |
| UpdateTitleAlignment(); | |
| UpdateToolbarButton(); | |
| } | |
| private void OnDisposed() | |
| { | |
| var navPage = _navPage; | |
| if (navPage != null) | |
| { | |
| navPage.Popped -= OnPageChanged; | |
| navPage.PoppedToRoot -= OnPageChanged; | |
| } | |
| if (_navigationButton != null) | |
| { | |
| _navigationButton.Click -= OnNavigationButtonClick; | |
| } | |
| } | |
| private async void OnPageChanged(object sender, NavigationEventArgs e) | |
| { | |
| await Task.Delay(20); | |
| UpdateTitleAlignment(); | |
| UpdateToolbarButton(); | |
| } | |
| private void UpdateTitleAlignment() | |
| { | |
| if(!IsCurrentPageAppearing())return; | |
| var toolbar = _toolbar; | |
| if (toolbar == null) return; | |
| for (var i = 0; i < toolbar.ChildCount; i++) | |
| { | |
| var view = toolbar.GetChildAt(i) as TextView; | |
| if (view != null) | |
| { | |
| if (_element.IsTitleCenter) | |
| { | |
| view.Gravity = GravityFlags.CenterHorizontal; | |
| var param = (Toolbar.LayoutParams)view.LayoutParameters; | |
| param.Width = ViewGroup.LayoutParams.MatchParent; | |
| //param.Width = toolbar.Width - 16; | |
| toolbar.SetPadding((int)ScreenHelper.ToPixel(-5),0,0,0); | |
| } | |
| else | |
| { | |
| toolbar.SetPadding(0,0,0,0); | |
| view.Gravity = GravityFlags.Left; | |
| var param = (Toolbar.LayoutParams)view.LayoutParameters; | |
| param.Width = ViewGroup.LayoutParams.WrapContent; | |
| } | |
| toolbar.RequestLayout(); | |
| } | |
| } | |
| } | |
| private void UpdateToolbarButton() | |
| { | |
| if (!IsCurrentPageAppearing()) return; | |
| var toolbar = _toolbar; | |
| if (toolbar == null) return; | |
| foreach (var toolbarItem in _element.ToolbarItems.ToList()) | |
| { | |
| if (toolbarItem.Priority == 0 && !string.IsNullOrEmpty(toolbarItem.Icon)) | |
| { | |
| _element.ToolbarItems.Remove(toolbarItem); | |
| _leftButton = toolbarItem; | |
| } | |
| } | |
| if (_leftButton != null) | |
| { | |
| toolbar.NavigationIcon = ResourceManager.GetDrawable(Context.Resources, _leftButton.Icon + ".png"); | |
| toolbar.SetContentInsetsAbsolute(0, 0); | |
| for (var i = 0; i < toolbar.ChildCount; i++) | |
| { | |
| var imageButton = toolbar.GetChildAt(i) as ImageButton; | |
| if (imageButton != null) | |
| { | |
| _navigationButton = imageButton; | |
| imageButton.Click -= OnNavigationButtonClick; | |
| imageButton.Click += OnNavigationButtonClick; | |
| return; | |
| } | |
| } | |
| return; | |
| } | |
| toolbar.SetContentInsetsAbsolute((int)ScreenHelper.ToPixel(ToolbarLeftOffset), 0); | |
| if (toolbar.NavigationIcon != null) | |
| { | |
| toolbar.SetNavigationIcon(Resource.Drawable.abc_ic_ab_back_mtrl_am_alpha); | |
| } | |
| else | |
| { | |
| toolbar.NavigationIcon = null; | |
| } | |
| } | |
| private void OnNavigationButtonClick(object sender, EventArgs e) | |
| { | |
| if (IsCurrentPageAppearing() && _leftButton != null) | |
| { | |
| var leftButtonController = (IMenuItemController) _leftButton; | |
| leftButtonController.Activate(); | |
| } | |
| } | |
| private bool IsCurrentPageAppearing() | |
| { | |
| var navPage = _navPage; | |
| return navPage != null && navPage.CurrentPage == _element; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment