语言:LayaBox - AS3
代码
package UIEngine.UICell
{
import laya.events.Event;
import laya.ui.List;
import laya.utils.Handler;
//用于List,UICell组控制
public class UICellGroup
{
//分散的组
// public var m_length:int = 0; //数量
public var m_cells:Array = []; //cell
public var m_celldatas:Array = []; //celldata
public var m_selectIndex:int = -1; //当前选中索引
//list
// public static var m_list:List = null; //名称
//menu
public var m_preIndex:int = 0; //上次选中
public var m_menu:List; //菜单
public var m_menuData:Array; //当前数据
public var m_parentData:Array; //一级菜单
public var m_childData:Array; //二级菜单
public var m_bExtand:Boolean = false; //是否展开
public function UICellGroup()
{
}
public function clearData():void
{
// m_cells = [];
m_celldatas = [];
m_selectIndex = -1;
}
public function addCell(cell:UICell):void
{
m_cells.push(cell);
}
public function getCell(index:int):UICell
{
return m_cells[index];
}
public function addCellData(celldata:UICellData):void
{
m_celldatas.push(celldata);
}
public function getCellData(index:int):UICellData
{
return m_celldatas[index];
}
public function Select(index:int):void
{
}
public function onSelect(index:int):void
{
if(m_selectIndex == index){
return;
}
var preIndex:int = m_selectIndex;
var precell:UICell = m_cells[preIndex];
var preData:* = m_celldatas[preIndex];
if(precell && preData){
preData.select = false;
precell.dataSource = preData;
}
var cell:UICell = m_cells[index];
var selectData:* = m_celldatas[index];
if(cell && selectData){
selectData.select = true;
cell.dataSource = selectData;
m_selectIndex = index;
}
}
public function refresh():void
{
for(var i:int=0;i<m_cells.length;i++){
(m_cells[i] as UICell).dataSource = m_celldatas[i];
}
}
public function RefreshSingle(index:int):void
{
var cell:UICell = m_cells[index];
if(cell){
cell.dataSource = m_celldatas[index];
}
}
//封装一下List,节省代码
public static function initList(list:List,itemRender:*,selectEnable:Boolean,selectHandler:Handler=null,mouseHandler:Handler=null):void
{
// m_list = list;
list.itemRender = itemRender;
list.selectEnable = selectEnable;//设置 list 可选。
//以下通常只用1个
if(selectHandler){
list.selectHandler = selectHandler;
}
if(mouseHandler){
list.mouseHandler = mouseHandler;
}
list.tag = -1; //这个用于记录上次点击的索引
}
public static function AutoSelectList(list:List,index:int):void
{
list.tag = -1;
if(list.selectHandler){
if(list.selectedIndex!=index){
list.selectedIndex = index;
}
else{
list.selectHandler.runWith(index);
}
}
else if(list.mouseHandler){
var event:Event = new Event();
event.type = "click";
list.mouseHandler.runWith([event,index]);
}
}
//改变选中状态,比如外观,默认单选
public static function onListSelect(list:List,listData:Array,index:int,multiSelect:Boolean=false):void
{
if(!multiSelect){
var preIndex:int = list.tag;
var preData:* = listData[preIndex];
if(preData){
preData.select = false;
list.changeItem(preIndex,preData);
}
var selectData:* = listData[index];
if(selectData){
selectData.select = true;
list.changeItem(index,selectData);
list.tag = index;
}
}
else{
var selectData2:* = listData[index];
if(selectData2){
selectData2.select = !selectData2.select;
list.changeItem(index,selectData2);
list.tag = index;
}
}
}
public function initMenu(list:List,itemRender:*,ckickHandler:Handler=null):void
{
m_menu = list;
list.itemRender = itemRender;
list.selectEnable = true;
if(ckickHandler){
list.mouseHandler = ckickHandler;
}
}
public function setMenuData(parentMenu:Array,childMenu:Array):void
{
m_parentData = parentMenu;
m_childData = childMenu;
m_menuData = m_parentData;
m_menu.dataSource = m_menuData;
}
public function getMenu(index:int):UICellData
{
return m_menuData[index];
}
public function onMenuSelect(index:int):void
{
var preType:int = -1;
var preData:UICellData = m_menuData[m_preIndex];
if(preData){
preType = preData.type == 0?preData.key:preData.type;
}
var selectData:UICellData = m_menuData[index];
var curType:int = selectData.type == 0?selectData.key:selectData.type;
var parentMenu:UICellData = selectData.type == 0?selectData:m_menuData[selectData.type];
var bParentChanged:Boolean=false; //是否更换父菜单
var bReload:Boolean = false; //是否重新加载数据
var firstChildIndex:int = -1;
if(preType == curType){
if(selectData.type == 0){
bReload = true;
m_bExtand = !m_bExtand;
}
else{
preData.select = false;
m_menu.changeItem(m_preIndex,preData);
selectData.select = true;
m_menu.changeItem(index,selectData);
m_preIndex = index;
return;
}
}
else{
bParentChanged = true;
bReload = true;
m_bExtand = true;
}
if(bReload){
var num:int=0;
if(m_bExtand){
m_menuData = [];
for each(var data:UICellData in m_parentData)
{
data.select = false;
data.index = num;
data.style = 0;
m_menuData.push(data);
num++;
if(data.key == curType){
for each(var data2:UICellData in m_childData)
{
if(data2.type == selectData.key){
data2.index = num;
data2.select = false;
m_menuData.push(data2);
num++;
}
}
}
}
}
else{
m_menuData = m_parentData;
for each(var data3:UICellData in m_menuData)
{
data3.select = false;
data3.index = num;
num++;
}
}
//切换状态和第一次子菜单
for each(var menu:UICellData in m_menuData)
{
if(menu.type==0){
if(menu.key == curType){
menu.select = true;
menu.style = m_bExtand?1:0;
if(!m_bExtand){
m_preIndex = menu.index;
}
}
}
else{
if(menu.type == curType && firstChildIndex<0){
menu.select = true;
firstChildIndex = menu.index;
}
}
}
}
m_menu.dataSource = m_menuData;
if(firstChildIndex>0){
m_preIndex = firstChildIndex;
AutoSelectMenu(firstChildIndex,true);
//父节点置顶
m_menu.scrollTo(firstChildIndex-1);
}
}
public function AutoSelectMenu(index:int,notRefreshMenu:Boolean=false):void
{
var event:Event = new Event();
event.type = "click";
m_menu.mouseHandler.runWith([event,index,notRefreshMenu]);
}
////////////////////////////////////////
public function getMenuDataIndex(data:*):int
{
for(var i:int=0;i<m_menuData.length;i++)
{
if(m_menuData[i]==data)
return i;
}
return -1;
}
public function getChildDataByKey(key:int):*
{
for(var i:int=0;i<m_childData.length;i++)
{
if(m_childData[i].key!=null)
{
if(m_childData[i].key==key)
{
return m_childData[i];
}
}
}
return null;
}
public function getParentDataByKey(key:int):*
{
for(var i:int=0;i<m_parentData.length;i++)
{
if(m_parentData[i].key!=null)
{
if(m_parentData[i].key==key)
{
return m_parentData[i];
}
}
}
return null;
}
/////////////////////////////////////////
}
}
使用示例
UICellGroup.initList(m_list,TacticalCell,true,null,Handler.create(this, onListSelect,null,false));
m_list.dataSource=m_arrSkillCellData;
UICellGroup.AutoSelectList(m_list,_selectListIndex);
private function onListSelect(e:Event,index:int):void{
if(e.type==Event.CLICK){
//修改列表项选中状态(cell中可以通过select变量判断是否被选中)
UICellGroup.onListSelect(m_list,m_arrSkillCellData,index);
}
}
配套cellData,列表项继承该类
package UIEngine.UICell
{
//通用CellData结构,可满足大部分UICell应用
//如果参数不足,可以继续添加arg1,arg2;
public class UICellData
{
public var index:int = 0; //索引
public var key:Number = 0; //uid
public var name:String = ""; //名称
public var icon:int = 0; //图标
public var type:int = 0; //类型
public var style:int = 0; //样式
//选中功能
public var select:Boolean = false; //是否选中
public var selectType:int = 0; //选中类型,1normal和pushed图片切换,2附加选中图片,3附加选中特效
public var normalRes:String = ""; //选中图片url,注意不是normalImage和pushedImage
public var selectRes:String = ""; //选中图片url,注意不是normalImage和pushedImage
public var offsetX:int = 0; //选中图片偏移,默认居中
public var offsetY:int = 0; //选中图片偏移,默认居中
public var zOrder:int = 0; //选中图片偏移cengji
//
public var arg:* = null; //自定义参数
public var arg1:* = null; //自定义参数
// public var flag:int=0; //标志 0:无 1:可合成
public var redType:int=0;//追踪红点类型
public var redID:int=0;//红点ID
public var redGroupid:int=-1;
public var redKey:int=-1;
public function UICellData(index:int=0)
{
this.index = index;
}
public function clear():void
{
index = 0;
key = 0;
name = "";
type = 0;
style = 0;
select = false;
arg = null;
}
//redType:0点 1上级 2组
public function setRedType(type:int,id:int,groupid:int=1,key:int=-1):void
{
this.redType = type;
this.redID = id;
this.redGroupid = groupid;
this.redKey = key;
}
public function setSelectType(type:int,selectRes:String = "",offsetX:Number=0,offsetY:Number=0,zOrder:int=0):void
{
this.selectType = type;
// this.normalRes = normalRes;
this.selectRes = selectRes;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.zOrder = zOrder;
}
}
}