using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Util; using Android.Views.Animations; namespace Shared { /// /// 当前视图 /// public class Spinner : Button { public Spinner() { TextColor = 0xff007aff; Radius = 10; BorderWidth = (uint)DensityUtil.Dip2Px(1); MouseUpEventHandler += (sender, e) => { if (AdapterStr == null || AdapterStr.Length == 0) { return; } ScrollView tempScrolView = new ScrollView(Application.Activity); tempScrolView.VerticalScrollBarEnabled = false; Application.RootFrameLayout.AddView(tempScrolView, new Android.Views.ViewGroup.LayoutParams(Application.CurrentWidth, Application.CurrentHeight)); tempScrolView.BringToFront(); tempScrolView.Alpha = 0.94f; Android.Widget.LinearLayout linearLayout = new Android.Widget.LinearLayout(Application.Activity); linearLayout.Orientation = Orientation.Vertical; tempScrolView.AddView(linearLayout, new Android.Views.ViewGroup.LayoutParams(tempScrolView.LayoutParameters.Width, Android.Views.ViewGroup.LayoutParams.WrapContent)); int height = 40; int totalHeight = AdapterStr.Length * DensityUtil.Dip2Px(height + 1); if (totalHeight < tempScrolView.LayoutParameters.Height) { var ll = new Android.Widget.TextView(Application.Activity); ll.Touch += (sender1, e1) => { if (e1.Event.Action == MotionEventActions.Up) { ((Android.Views.ViewGroup)tempScrolView.Parent).RemoveView(tempScrolView); } }; linearLayout.AddView(ll, new Android.Views.ViewGroup.LayoutParams(tempScrolView.LayoutParameters.Width, tempScrolView.LayoutParameters.Height - totalHeight)); } Android.Widget.LinearLayout l = new Android.Widget.LinearLayout(Application.Activity); l.Orientation = Orientation.Vertical; linearLayout.AddView(l, new Android.Views.ViewGroup.LayoutParams(linearLayout.LayoutParameters.Width, Android.Views.ViewGroup.LayoutParams.WrapContent)); for (int i = 0; i < this.AdapterStr.Length; i++) { Android.Widget.Button button = new Android.Widget.Button(Application.Activity); button.Text = AdapterStr[i]; button.SetTextColor(new Android.Graphics.Color(0x6d, 0x6d, 0x72, 0xff)); button.SetBackgroundColor(Android.Graphics.Color.White); button.Tag = i; button.Click += (sender1, e1) => { ((Android.Views.ViewGroup)tempScrolView.Parent).RemoveView(tempScrolView); //tempScrolView.Animation = new TranslateAnimation( // Dimension.RelativeToSelf, 0.0f, // Dimension.RelativeToSelf, 0.0f, // Dimension.RelativeToSelf, 0.0f, // Dimension.RelativeToSelf, 1.0f); //tempScrolView.Animation.Duration = 500; ////view.Animation.AnimationEnd += (sender, e) => ////{ //// ((Android.Views.ViewGroup)view.Parent).RemoveView(view); ////}; //tempScrolView.Animation.StartNow(); if (SelectedItemChanged != null) { SelectedItemChanged(this, (int)button.Tag); Text = AdapterStr[(int)button.Tag]; } }; l.AddView(button, new Android.Views.ViewGroup.LayoutParams(l.LayoutParameters.Width, DensityUtil.Dip2Px(height))); if (i == AdapterStr.Length - 1) { //var gradientDrawable = new Android.Graphics.Drawables.GradientDrawable(); //gradientDrawable.SetCornerRadius(10); //button.Background = gradientDrawable; } else { Android.Widget.TextView line = new Android.Widget.TextView(Application.Activity); line.SetBackgroundColor(new Android.Graphics.Color(0xdd, 0xdd, 0xdd, 0xff)); l.AddView(line, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, DensityUtil.Dip2Px(1))); if (i == 0) { //var gradientDrawable = new Android.Graphics.Drawables.GradientDrawable(); //gradientDrawable.SetCornerRadius(10); //button.Background = gradientDrawable; } } } tempScrolView.Animation = new TranslateAnimation( Dimension.RelativeToSelf, 0.0f, Dimension.RelativeToSelf, 0.0f, Dimension.RelativeToSelf, 1.0f, Dimension.RelativeToSelf, 0.0f); tempScrolView.Animation.Duration = 500; tempScrolView.Animation.StartNow(); }; } int currentRow; /// /// 当前行 /// /// 当前行,从0开始 public int CurrentRow { get { return currentRow; } set { if (AdapterStr == null || value < 0 || AdapterStr.Length <= value) { return; } currentRow = value; Text = AdapterStr[value]; } } /// /// 添加下拉列表的数据 /// /// String. public string[] AdapterStr { get; set; } /// /// 选择列表变化的事件 /// public Action SelectedItemChanged; /// /// 刷新当前视图 /// public override void Refresh() { base.Refresh(); CurrentRow = 0; } } }