using System;
using Shared;
using HDL_ON.UI.Music;
using System.Collections.Generic;
using HDL_ON.UI.UI2.FuntionControlView.Aks.Entity;
namespace HDL_ON.UI.UI2.FuntionControlView.Aks.CommonView
{
///
/// 水平滑动容器
///
public class HorizontalFramLayout : HorizontalScrolViewLayout
{
///
///
///
/// 容器宽度
/// 容器高度
public HorizontalFramLayout(int width, int height)
{
this.Height = Application.GetRealHeight(height);
this.Width = Application.GetRealWidth(width);
}
///
/// 选择事件
///
public Action SelectTypeEvent = null;
///
/// 初始化控件
///
/// 显示数据列表
/// 默认值索引范围[0,mList.count-1],大于范围,该值视为无效
public void InitControl(List mList, int defaultIndex = -1)
{
if (mList == null || mList.Count == 0)
{
return;
}
bool isBool = false;
Button btnSelected = new Button();
for (int i = 0; i < mList.Count; i++)
{
var filters = mList[i];
Button button = new Button
{
Height = Application.GetRealHeight(28),
Width = Application.GetRealWidth(20),
SelectedBackgroundColor = MusicColor.MusicTxet14SelectedColor,
BackgroundColor = 0x00000000,
Text = mList[i].filterName,
TextSize = TextSize.Text14,
TextColor = MusicColor.TextColor,
SelectedTextColor = MusicColor.WhiteColor,
TextAlignment = TextAlignment.Center,
Padding = new Padding(4, 4, 4, 4),
Radius = (uint)Application.GetRealHeight(4),
Tag = filters,
Name = i.ToString(),
};
//重新计算组件宽度
button.Width = button.GetTextWidth() + Application.GetRealWidth(4 + 4);
this.AddChidren(button);
//间隔,弄个空的进去占位置
Button btnSpacing = new Button
{
Height = Application.GetRealHeight(28),
Width = Application.GetRealWidth(20),
BackgroundColor = 0x00000000,
};
this.AddChidren(btnSpacing);
button.MouseDownEventHandler += (sen, e) =>
{
if (btnSelected.Name == button.Name)
{
//点击同一个组件无效
return;
}
btnSelected.IsSelected = false;
button.IsSelected = true;
btnSelected = button;
if (button.Tag == null || !(button.Tag is Filters))
{
return;
}
var filters = (Filters)button.Tag;
SelectTypeEvent?.Invoke(filters);
};
if (defaultIndex != -1 && defaultIndex == i)
{
btnSelected.IsSelected = false;
button.IsSelected = true;
btnSelected = button;
isBool = true;
}
}
if (isBool)
{
if (defaultIndex < mList.Count)
{
//默认索引回调,初始化时执行
SelectTypeEvent?.Invoke(mList[defaultIndex]);
}
}
}
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
this.SelectTypeEvent = null;
base.RemoveFromParent();
}
}
}