Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using Xamarin.Forms;
- namespace Xam.Plugin.ETL
- {
- public class ExpandableTabLayout : Layout<View>
- {
- public static readonly BindableProperty TabCountProperty = BindableProperty.Create(nameof(TabCount), typeof(int), typeof(ExpandableTabLayout), 0, propertyChanged: OnTabCountChanged);
- public static readonly BindableProperty FocusedViewProperty = BindableProperty.Create(nameof(FocusedView), typeof(View), typeof(ExpandableTabLayout), null, propertyChanged: OnFocusedViewChanged);
- private static void OnFocusedViewChanged(BindableObject bindable, object oldValue, object newValue)
- {
- }
- private static void OnTabCountChanged(BindableObject bindable, object oldValue, object newValue)
- {
- // Handle animations and applying the new tab
- var self = (ExpandableTabLayout) bindable;
- self.InvalidateMeasure();
- }
- public View FocusedView
- {
- get { return (View)GetValue(FocusedViewProperty); }
- set { SetValue(FocusedViewProperty, value); }
- }
- public int TabCount
- {
- get { return (int) GetValue(TabCountProperty); }
- set { SetValue(TabCountProperty, value); }
- }
- private bool TabsDisplayed = false;
- protected override void OnChildAdded(Element child)
- {
- if (FocusedView == null)
- FocusedView = child as View;
- base.OnChildAdded(child);
- }
- protected override void LayoutChildren(double x, double y, double width, double height)
- {
- SortStack();
- if (!TabsDisplayed)
- {
- foreach (var child in Children)
- {
- if (child != FocusedView)
- child.InputTransparent = true;
- LayoutChildIntoBoundingRegion(child, new Rectangle(x, y, width, height));
- }
- }
- }
- /// <summary>
- /// Sorts the stack based on the FocusedView
- /// </summary>
- private void SortStack()
- {
- for (int i = 0; i != Children.Count; i++)
- {
- if (Children[i] != FocusedView)
- LowerChild(Children[i]);
- }
- RaiseChild(FocusedView);
- }
- public void ToggleTabView()
- {
- TabsDisplayed = !TabsDisplayed;
- if (TabsDisplayed)
- DisplayTabs();
- else
- HideTabs();
- }
- private async void HideTabs()
- {
- // Hide unselected first
- foreach (var child in Children)
- {
- if (child != FocusedView)
- {
- }
- }
- // Display Selected
- await FocusedView.ScaleTo(1, 500, Easing.SpringIn);
- // Place unselected into normal scale for later
- foreach (var child in Children)
- {
- if (child != FocusedView)
- {
- child.Scale = 1;
- }
- }
- // Invalidate and redraw
- InvalidateLayout();
- }
- private void DisplayTabs()
- {
- foreach (var child in Children)
- {
- child.ScaleTo(0.75, 500, Easing.SpringOut);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement