黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// <para>场景和功能之间相互切换的Tab控件(完成初始化后,会根据默认选择调用回调函数)</para>
    /// <para>2020.03.10变更:它变更为文字型菜单控件</para>
    /// </summary>
    public class SceneFunctionSwitchControl : HorizontalScrolViewLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 选择事件(从0开始)
        /// </summary>
        public Action<int> SelectTabEvent = null;
        /// <summary>
        /// 前回选择的索引
        /// </summary>
        public int OldSelectIndex = 0;
 
        private int m_nowSelectIndex = 0;
        /// <summary>
        /// 当前选择的分支(从0开始)
        /// </summary>
        public int nowSelectIndex
        {
            get { return m_nowSelectIndex; }
        }
 
        /// <summary>
        ///  所有名字的控件
        /// </summary>
        private List<NormalViewControl> listNameControl = new List<NormalViewControl>();
        /// <summary>
        /// 所有线的控件
        /// </summary>
        private List<NormalViewControl> listLineControl = new List<NormalViewControl>();
        /// <summary>
        /// 默认的那个菜单
        /// </summary>
        private FrameLayoutStatuControl frameDefult = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 场景和功能之间相互切换的Tab控件(完成初始化后,会根据默认选择调用回调函数)
        /// </summary>
        public SceneFunctionSwitchControl()
        {
            this.Height = Application.GetRealHeight(90);
        }
 
        /// <summary>
        ///  初始化控件(事件要至于它之前)
        /// </summary>
        /// <param name="listTitleText">菜单文字列表</param>
        /// <param name="rightSpace">右边空白宽度</param>
        public void InitControl(List<string> listTitleText, int rightSpace = 0)
        {
            if (listTitleText.Count == 0) { return; }
            this.listNameControl.Clear();
            this.listLineControl.Clear();
 
            var btnTemp = new NormalViewControl(10, 10, false);
            btnTemp.TextSize = 16;
 
            //做成头部菜单文字
            for (int i = 0; i < listTitleText.Count; i++)
            {
                btnTemp.Text = listTitleText[i];
                //添加标题控件
                this.AddTitleTextControl(i, listTitleText[i], btnTemp.GetRealWidthByText());
            }
            //添加右边空白宽度
            if (rightSpace > 0)
            {
                //添加一个间隔间距
                var frameSpace = new FrameLayout();
                frameSpace.Width = Application.GetRealWidth(rightSpace);
                this.AddChidren(frameSpace);
            }
            //回调方法
            this.frameDefult.ButtonClickEvent?.Invoke(null, null);
        }
 
        /// <summary>
        /// 添加标题控件
        /// </summary>
        /// <param name="i_index"></param>
        /// <param name="titleText"></param>
        private void AddTitleTextControl(int i_index, string titleText, int realWidth)
        {
            //添加一个间隔间距
            var frameSpace = new FrameLayout();
            frameSpace.Width = Application.GetRealWidth(58);
            this.AddChidren(frameSpace);
 
            //场景框(点击范围有点小,需要扩大它的点击范围)
            var frameBack = new FrameLayoutStatuControl();
            frameBack.UseClickStatu = false;
            frameBack.Width = realWidth;
            this.AddChidren(frameBack);
            //名称
            var btnName = new NormalViewControl(frameBack.Width, Application.GetRealHeight(63), false);
            btnName.Text = titleText;
            btnName.TextAlignment = TextAlignment.Center;
            if (m_nowSelectIndex == i_index)
            {
                this.frameDefult = frameBack;
                //选择状态
                btnName.TextSize = 16;
                btnName.IsBold = true;
                btnName.TextColor = UserCenterColor.Current.TextColor2;
            }
            else
            {
                btnName.TextColor = UserCenterColor.Current.TextGrayColor3;
            }
            frameBack.AddChidren(btnName, ChidrenBindMode.BindEvent);
            //添加缓存
            this.listNameControl.Add(btnName);
 
            //底线
            var btnLine = new NormalViewControl(40, 10, true);
            btnLine.Y = btnName.Bottom + Application.GetRealHeight(6);
            btnLine.X = btnName.X + btnName.Width / 2 - Application.GetRealWidth(40) / 2;
            btnLine.Radius = (uint)Application.GetRealHeight(10) / 2;
            btnLine.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            frameBack.AddChidren(btnLine, ChidrenBindMode.BindEvent);
            if (m_nowSelectIndex != i_index)
            {
                btnLine.Visible = false;
            }
            //添加缓存
            this.listLineControl.Add(btnLine);
 
            frameBack.ButtonClickEvent += (sender, e) =>
            {
                //选择的是同一个分支,则无效
                if (this.m_nowSelectIndex == i_index && e != null)
                {
                    //手动点击的才处理
                    return;
                }
                this.OldSelectIndex = this.m_nowSelectIndex;
                this.m_nowSelectIndex = i_index;
                //当前文字变色
                btnName.TextSize = 16;
                btnName.TextColor = UserCenterColor.Current.TextColor2;
                btnName.IsBold = true;
                btnLine.Visible = true;
 
                //还原其他的颜色
                for (int i = 0; i < listNameControl.Count; i++)
                {
                    if (this.m_nowSelectIndex != i)
                    {
                        listNameControl[i].TextSize = 14;
                        listNameControl[i].IsBold = false;
                        listNameControl[i].TextColor = UserCenterColor.Current.TextGrayColor3;
 
                        listLineControl[i].Visible = false;
                    }
                }
                this.SelectTabEvent?.Invoke(i_index);
            };
        }
 
        #endregion
 
        #region ■ 控件摧毁___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            this.SelectTabEvent = null;
            if (this.Parent != null)
            {
                base.RemoveFromParent();
            }
        }
 
        /// <summary>
        /// ☆☆移除全部控件☆☆
        /// </summary>
        public override void RemoveAll()
        {
            if (this.Parent != null)
            {
                base.RemoveAll();
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 设置初始选择
        /// </summary>
        /// <param name="index">从0开始</param>
        public void SetDefultIndex(int index)
        {
            if (frameDefult != null && frameDefult.Parent != null)
            {
                if (index < this.listNameControl.Count)
                {
                    this.ScrollToViewIndex(index * 2 + 1);
                    //调用点击事件
                    this.listNameControl[index].ButtonClickEvent(null, null);
                }
                return;
            }
            this.m_nowSelectIndex = index;
        }
 
        #endregion
    }
}