wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
 
using System;
 
namespace Shared
{
    /// <summary>
    /// Button 按键
    /// </summary>
    public class ViewGroup : View
    {
        /// <summary>
        /// 真正的容器
        /// </summary>
        internal Android.Views.ViewGroup realViewGroup;
 
        /// <summary>
        /// 当前控件
        /// </summary>
        /// <value>The android frame layout.</value>
        internal Android.Views.ViewGroup viewGroup
        {
            get
            {
                return AndroidView as Android.Views.ViewGroup;
            }
            set
            {
                AndroidView = value;
            }
        }
    
        /// <summary>
        /// 增加子控件
        /// </summary>
        /// <param name="view">View.</param>
        public virtual void AddChidren(View view)
        {
            view.Parent = this;
            viewList.Add(view);
            if (GetType () == typeof (VerticalScrolViewLayout)) {
                //这个是为了填充的
                if (0<realViewGroup.ChildCount&&realViewGroup.GetChildAt (realViewGroup.ChildCount - 1).Tag != null&&realViewGroup.GetChildAt (realViewGroup.ChildCount - 1).Tag.ToString()=="填充") {
                    realViewGroup.RemoveViewAt (realViewGroup.ChildCount - 1);
                }
            }
            if(view.AndroidView.Parent!=null){
                (view.AndroidView.Parent as Android.Views.ViewGroup)?.RemoveView(view.AndroidView);
            }
            realViewGroup.AddView(view.AndroidView);
            if (!IsCanRefresh)
            {
                return;
            }
            view.Refresh();
 
            if (view is GestureLockView) {
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(100);
                    Shared.Application.RunOnMainThread(() =>
                    {
                        (view as GestureLockView).Height = view.Height+1;
                        (view as GestureLockView).Height = view.Height - 1;
                    });
                }).Start();
            }
 
            //如果父控件是这种,就在最后补上一个控件
            if (GetType () == typeof (VerticalScrolViewLayout)) {
                int tempHeight = -10;
                foreach (var temp in viewList) {
                    tempHeight += temp.Height;
                }
                realViewGroup.AddView (new Android.Widget.LinearLayout (Application.Activity) { Tag = "填充" }, new Android.Views.ViewGroup.LayoutParams (Android.Views.ViewGroup.LayoutParams.MatchParent, tempHeight < realViewGroup.LayoutParameters.Height ? realViewGroup.LayoutParameters.Height - tempHeight:0));
            }
 
            addParentToAllSubViews(view);
 
            if (view is ViewGroup)
            {
                var tempViewGroup = (ViewGroup)view;
                for (int i = 0; i < tempViewGroup.ChildrenCount; i++)
                {
                    tempViewGroup.GetChildren(i).Refresh();
                }
            }
        }
 
        internal void addParentToAllSubViews(View view)
        {
            if (view is ViewGroup)
            {
                var viewgroup = view as ViewGroup;
                for (int i = 0; i < viewgroup.viewList.Count; i++)
                {
                    var _view = viewgroup.viewList[i];
                    if (_view == null) continue;
                    _view.Parent = viewgroup;
 
                    addParentToAllSubViews(_view);//递归,将子控件所包含的控件也一并移除;
                }
            }
        }
 
        /// <summary>
        /// 控件数量
        /// </summary>
        /// <value>The children count.</value>
        public int ChildrenCount { get { return viewList.Count; } }
 
        /// <summary>
        /// 控件列表
        /// </summary>
        internal System.Collections.Generic.List<View> viewList = new System.Collections.Generic.List<View>();
 
 
        string backgroundImagePath;
        /// <summary>
        /// 背景图片
        /// </summary>
        /// <value>The background image path.</value>
        public virtual string BackgroundImagePath
        {
            get
            {
                return backgroundImagePath;
            }
            set
            {
                backgroundImagePath = value;
                if (!IsCanRefresh || null == backgroundImagePath || Radius != 0)
                {
                    return;
                }
                if (Background(backgroundImagePath))
                {
                    
                }
            }
        }
 
        /// <summary>
        /// 刷新界面
        /// </summary>
        public override void Refresh()
        {
            base.Refresh();
            BackgroundImagePath = backgroundImagePath;
        }
 
        /// <summary>
        /// 移除控件
        /// </summary>
        /// <param name="view">View.</param>
        internal virtual void Remove(View view)
        {
            if (view == null)
            {
                return;
            }
 
            viewList.Remove(view);
            realViewGroup.RemoveView(view.AndroidView);
            view.Parent = null;
        }
 
        internal virtual void removeChildParent()
        {
            for (int i = 0; i < viewList.Count; i++)
            {
                var view = viewList[i];
                if (view == null) continue;
                if (view is ViewGroup)
                {
                    (view as ViewGroup).removeChildParent();
                }
                view.Parent = null;
                //lock (dimageCached)
                //{
                //    foreach (var keyValue in dimageCached)
                //    {
                //        if (keyValue.Key.StartsWith(view.GetHashCode() + "_", StringComparison.Ordinal))
                //        {
                //            dimageCached.Remove(keyValue.Key);
                //            keyValue.Value.Dispose();
                //            break;
                //        }
                //    }
                //}
            }
        }
 
        public override void RemoveFromParent()
        {
            removeChildParent();
            base.RemoveFromParent();
        }
 
 
 
        /// <summary>
        /// 清空所有的控件
        /// </summary>
        public virtual void RemoveAll()
        {
            while (0 < viewList.Count)
            {
                //Remove(GetChildren(0));
                GetChildren(0)?.RemoveFromParent();
            }
        }
 
        /// <summary>
        /// 移除指定索引对象
        /// </summary>
        /// <param name="index">Index.</param>
        public virtual void RemoveAt(int index)
        {
            //Remove(GetChildren(index));
            GetChildren(index)?.RemoveFromParent();
        }
 
        /// <summary>
        /// 获取指定索引对象
        /// </summary>
        /// <returns>The children.</returns>
        /// <param name="index">Index.</param>
        public View GetChildren(int index)
        {
            if (viewList.Count - 1 < index || index < 0)
            {
                return null;
            }
            return viewList[index];
        }
 
        /// <summary>
        /// 根据类型移除控件
        /// </summary>
        /// <param name="type">Type.</param>
        public void RemoveViewByType(System.Type type){
            for (int i = 0; i < viewList.Count;i++)
            {
                if(viewList[i].GetType()==type)
                {
                    viewList[i].RemoveFromParent();
                    i--;
                }
            }
        }
 
        /// <summary>
        /// 根据Tag移除控件
        /// </summary>
        /// <param name="type">Type.</param>
        public void RemoveViewByTag(object tag)
        {
            for (int i = 0; i < viewList.Count; i++)
            {
                if (viewList[i].Tag == tag)
                {
                    viewList[i].RemoveFromParent();
                    i--;
                }
            }
        }
    }
}