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;
|
}
|
}
|
}
|