using System;
namespace Shared
{
///
/// Button 按键
///
public class ViewGroup : View
{
///
/// 真正的容器
///
internal Android.Views.ViewGroup realViewGroup;
///
/// 当前控件
///
/// The android frame layout.
internal Android.Views.ViewGroup viewGroup
{
get
{
return AndroidView as Android.Views.ViewGroup;
}
set
{
AndroidView = value;
}
}
///
/// 增加子控件
///
/// View.
public virtual void AddChidren(View view)
{
view.Parent = this;
viewList.Add(view);
if (GetType () == typeof (VerticalScrolViewLayout)) {
//这个是为了填充的
if (0
{
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);//递归,将子控件所包含的控件也一并移除;
}
}
}
///
/// 控件数量
///
/// The children count.
public int ChildrenCount { get { return viewList.Count; } }
///
/// 控件列表
///
internal System.Collections.Generic.List viewList = new System.Collections.Generic.List();
string backgroundImagePath;
///
/// 背景图片
///
/// The background image path.
public virtual string BackgroundImagePath
{
get
{
return backgroundImagePath;
}
set
{
backgroundImagePath = value;
if (!IsCanRefresh || null == backgroundImagePath || Radius != 0)
{
return;
}
if (Background(backgroundImagePath))
{
}
}
}
///
/// 刷新界面
///
public override void Refresh()
{
base.Refresh();
BackgroundImagePath = backgroundImagePath;
}
///
/// 移除控件
///
/// View.
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();
}
///
/// 清空所有的控件
///
public virtual void RemoveAll()
{
while (0 < viewList.Count)
{
//Remove(GetChildren(0));
GetChildren(0)?.RemoveFromParent();
}
}
///
/// 移除指定索引对象
///
/// Index.
public virtual void RemoveAt(int index)
{
//Remove(GetChildren(index));
GetChildren(index)?.RemoveFromParent();
}
///
/// 获取指定索引对象
///
/// The children.
/// Index.
public View GetChildren(int index)
{
if (viewList.Count - 1 < index || index < 0)
{
return null;
}
return viewList[index];
}
///
/// 根据类型移除控件
///
/// Type.
public void RemoveViewByType(System.Type type){
for (int i = 0; i < viewList.Count;i++)
{
if(viewList[i].GetType()==type)
{
viewList[i].RemoveFromParent();
i--;
}
}
}
///
/// 根据Tag移除控件
///
/// Type.
public void RemoveViewByTag(object tag)
{
for (int i = 0; i < viewList.Count; i++)
{
if (viewList[i].Tag == tag)
{
viewList[i].RemoveFromParent();
i--;
}
}
}
}
}