wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
{
    /// <summary>
    /// 当前视图
    /// </summary>
    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;
        /// <summary>
        /// 当前行
        /// </summary>
        /// <value>当前行,从0开始</value>
        public int CurrentRow
        {
            get
            {
                return currentRow;
            }
            set
            {
                if (AdapterStr == null || value < 0 || AdapterStr.Length <= value)
                {
                    return;
                }
 
                currentRow = value;
                Text = AdapterStr[value];
            }
        }
 
 
        /// <summary>
        /// 添加下拉列表的数据
        /// </summary>
        /// <param name="Str">String.</param>
        public string[] AdapterStr
        {
            get;
            set;
        }
 
        /// <summary>
        /// 选择列表变化的事件
        /// </summary>
        public Action<View, int> SelectedItemChanged;
 
 
        /// <summary>
        /// 刷新当前视图
        /// </summary>
        public override void Refresh()
        {
            base.Refresh();
            CurrentRow = 0;
        }
    }
}