xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 做成一个点击能够显示选中状态背景色的FrameLayout
    /// </summary>
    public class StatuFrameLayout : FrameLayout
    {
        /// <summary>
        /// Mouse up event.
        /// </summary>
        public delegate void _MouseUpEvent(object sender, MouseEventArgs e);
        /// <summary>
        /// 单击弹起事件
        /// </summary>
        public _MouseUpEvent MouseUpEvent;
        /// <summary>
        /// 状态设置前的事件
        /// </summary>
        public delegate void _SelectStatuEventBefore(bool select);
        /// <summary>
        /// 状态设置前的事件
        /// </summary>
        public _SelectStatuEventBefore SelectStatuEventBefore;
        /// <summary>
        /// 单击按下的坐标
        /// </summary>
        private System.Drawing.PointF downPoint = new System.Drawing.PointF();
        /// <summary>
        /// 子控件列表
        /// </summary>
        private List<Button> listView = new List<Button>();
        /// <summary>
        /// 子控件列表原来的字体颜色
        /// </summary>
        private List<uint> listViewColor = new List<uint>();
        /// <summary>
        /// 当前是否已经处于选择状态
        /// </summary>
        private bool IsSelectStatu = false;
 
        /// <summary>
        /// 做成一个点击能够显示选中状态背景色的FrameLayout
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public StatuFrameLayout(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            this.Width = i_Width;
 
            this.MouseUpEventHandler += ChildrenUpEvent;
            this.MouseDownEventHandler += ChildrenDownEvent;
        }
 
        /// <summary>
        /// 做成一个点击能够显示选中状态背景色的FrameLayout
        /// </summary>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public StatuFrameLayout(int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            this.Width = Application.CurrentWidth;
            this.MouseUpEventHandler += ChildrenUpEvent;
            this.MouseDownEventHandler += ChildrenDownEvent;
        }
 
        /// <summary>
        /// 添加子控件
        /// </summary>
        /// <param name="view">子控件</param>
        /// <param name="chidrenBindMode">绑定模式</param>
        public void AddChidren(View view, ChidrenBindMode chidrenBindMode = ChidrenBindMode.BindAll)
        {
            //绑定子控件事件
            this.BindChidrenEvent(view, chidrenBindMode);
 
            base.AddChidren(view);
        }
 
        /// <summary>
        /// 变更子控件的绑定模式
        /// </summary>
        /// <param name="view">子控件</param>
        /// <param name="chidrenBindMode">变更的绑定模式</param>
        public void ChangedChidrenBindMode(View view, ChidrenBindMode chidrenBindMode)
        {
            if (view is Button)
            {
                int index = listView.IndexOf((Button)view);
                if (index >= 0)
                {
                    listView.RemoveAt(index);
                    listViewColor.RemoveAt(index);
                }
 
                //子控件移除事件
                Button button = (Button)view;
                button.MouseUpEventHandler -= ChildrenUpEvent;
                button.MouseDownEventHandler -= ChildrenDownEvent;
 
                this.BindChidrenEvent(view, chidrenBindMode);
            }
        }
 
        /// <summary>
        /// 移除底层控件自身的单击事件
        /// </summary>
        public void RemoveBaseClickEvent()
        {
            this.MouseUpEventHandler -= ChildrenUpEvent;
            this.MouseDownEventHandler -= ChildrenDownEvent;
        }
 
        /// <summary>
        /// 绑定子控件事件
        /// </summary>
        /// <param name="view"></param>
        /// <param name="chidrenBindMode"></param>
        private void BindChidrenEvent(View view, ChidrenBindMode chidrenBindMode)
        {
            if (view is Button && chidrenBindMode != ChidrenBindMode.NotBind)
            {
                //为子控件添加事件
                Button button = (Button)view;
                button.MouseUpEventHandler -= ChildrenUpEvent;
                button.MouseDownEventHandler -= ChildrenDownEvent;
 
                button.MouseUpEventHandler += ChildrenUpEvent;
                button.MouseDownEventHandler += ChildrenDownEvent;
 
                if (chidrenBindMode == ChidrenBindMode.BindAll)
                {
                    listView.Add(button);
                    listViewColor.Add(button.TextColor);
                }
            }
        }
 
        /// <summary>
        /// 点击按下事件(点亮)
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void ChildrenDownEvent(object sender, MouseEventArgs e)
        {
            this.downPoint.X = e.X;
            this.downPoint.Y = e.Y;
 
            this.StartSelectStatuThread(ControlCommonResourse.StatuChangedWaitTime);
        }
 
        /// <summary>
        /// 点击松开事件
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void ChildrenUpEvent(object sender, MouseEventArgs e)
        {
            //移动误差为指定设定值时,判断为滑动行为,不执行弹起事件
            //if (this.IsMoveEvent(e.X, e.Y) == true)
            //{
            //    return;
            //}
            //调用委托
            if (MouseUpEvent != null)
            {
                MouseUpEvent(sender, e);
            }
        }
 
        /// <summary>
        /// 强制实施控件选中状态
        /// </summary>
        /// <param name="waiTime"></param>
        public void StartSelectStatuThread(int waiTime)
        {
            if (this.IsSelectStatu == true)
            {
                return;
            }
            this.IsSelectStatu = true;
 
            //设置选择状态
            this.SetSelectStatu();
            new System.Threading.Thread(() =>
            {
                System.Threading.Thread.Sleep(waiTime);
                Application.RunOnMainThread(() =>
                {
                    //设置不选择状态
                    this.SetNotSelectStatu();
                });
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 设置不选择状态
        /// </summary>
        public void SetNotSelectStatu()
        {
            if (this.SelectStatuEventBefore != null)
            {
                this.SelectStatuEventBefore(false);
            }
            for (int i = 0; i < listView.Count; i++)
            {
                //这是一个图片
                if (!string.IsNullOrEmpty(listView[i].UnSelectedImagePath))
                {
                    listView[i].IsSelected = false;
                }
                //这是一个文本
                else
                {
                    listView[i].TextColor = listViewColor[i];
                }
            }
            this.IsSelectStatu = false;
        }
 
        /// <summary>
        /// 设置选择状态
        /// </summary>
        public void SetSelectStatu()
        {
            if (this.SelectStatuEventBefore != null)
            {
                this.SelectStatuEventBefore(true);
            }
            for (int i = 0; i < listView.Count; i++)
            {
                //这是一个图片
                if (!string.IsNullOrEmpty(listView[i].SelectedImagePath))
                {
                    listView[i].IsSelected = true;
                }
                //这是一个文本
                else
                {
                    //覆盖之前的颜色,有可能它的颜色被变更了
                    listViewColor[i] = listView[i].TextColor;
                    listView[i].TextColor = UserCenterColor.Current.SelectTextColor;
                }
            }
        }
 
        /// <summary>
        /// 判断是否移动
        /// </summary>
        /// <param name="XX"></param>
        /// <param name="YY"></param>
        /// <returns></returns>
        private bool IsMoveEvent(float XX, float YY)
        {
            //移动误差为指定设定值时,判断为滑动行为,不执行弹起事件
            float moveValue = 100;
 
            float value1 = this.downPoint.X - XX;
            if (value1 < 0)
            {
                //转为正数
                value1 = -1 * value1;
            }
            float value2 = this.downPoint.Y - YY;
            if (value2 < 0)
            {
                //转为正数
                value2 = -1 * value2;
            }
            //移动误差为指定设定值时,判断为滑动行为,不执行弹起事件
            if (value1 >= moveValue || value2 >= moveValue)
            {
                return true;
            }
            return false;
        }
    }
}