-- 方法一:自己创建layer实现 (支持cell大小不一致)
-- 二级下拉菜单管理
friend_TwoLayer = {
}
friend_TwoLayer = class("friend_TwoLayer",function() return display.newLayer()
end)
function friend_TwoLayer:ctor()
printInfo("friend_TwoLayer:ctor.......")
end
function friend_TwoLayer:init(size,cellsize,Fdata,Cdata)
printInfo("friend_TwoLayer:init.......")
self.FCallBack = nil
self.CCallBack = nil
self.Fdata = Fdata
self.Cdata = Cdata
for i,vo in pairs(self.Fdata) do
vo._Index = i
end
self.CellData = clone(self.Fdata)
self.size = size
-- layer的大小
self:setContentSize(size.width,size.height)
local listener = cc.EventListenerTouchOneByOne:create()
listener:setSwallowTouches(true)
listener:registerScriptHandler(handler(self, self.onTouchBegan), cc.Handler.EVENT_TOUCH_BEGAN)
listener:registerScriptHandler(handler(self, self.onTouchMoved), cc.Handler.EVENT_TOUCH_MOVED)
listener:registerScriptHandler(handler(self, self.onTouchEnded), cc.Handler.EVENT_TOUCH_ENDED)
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
local function onNodeEvent(event)
if "enter" == event then
self:onEnter()
elseif "exit" == event then
self:onExit()
end
end
self:registerScriptHandler(onNodeEvent)
self.node = display.newNode()
self:addChild(self.node)
-- 上次点击父节点索引
self._LastCilckFIndex = 1
self:reloadData()
end
function friend_TwoLayer:createCell(index,vo)
local shareCell = ccui.ImageView:create("image/buttom/h_0001_.png")
self.node:addChild(shareCell)
shareCell:setAnchorPoint(0,0)
shareCell:setTouchEnabled(true)
shareCell:setSwallowTouches(false)
if not vo.IsC then
-- 对一级菜单处理
local text = ccui.Text:create("通过微信分享"..index, Tm.Res.FontDefault, 26)
text:setPosition(shareCell:getContentSize().width/2,shareCell:getContentSize().height/2)
shareCell:addChild(text)
shareCell:addClickEventListener(function ()
local _Index = self.CellData[index]._Index
self.CellData = {}
self._LastCilckFIndex = _Index
-- 数据重置计算
if not self.LastCilckFIndex or self.LastCilckFIndex ~= _Index then
for i,vo in pairs(self.Fdata) do
self.CellData[#self.CellData + 1] = clone(vo)
if _Index == vo._Index and self.Cdata[_Index] then
for i,cvo in pairs(self.Cdata[_Index]) do
cvo.IsC = true
self.CellData[#self.CellData + 1] = clone(cvo)
end
end
end
self.LastCilckFIndex = _Index
else
for i,vo in pairs(self.Fdata) do
self.CellData[#self.CellData + 1] = clone(vo)
end
self.LastCilckFIndex = nil
end
self:reloadData()
if self.FCallBack then
self.FCallBack(self.CellData[index])
end
end)
else
-- 对二级菜单处理
shareCell:loadTexture("image/buttom/asdasdas.png")
shareCell:addClickEventListener(function ()
self:addData({1},{{1}})
end)
end
return shareCell
end
function friend_TwoLayer:reloadData()
-- 清除移动node
self.node:removeAllChildren()
-- cell坐标,node的坐标
local statx,reloadY = 0,0
-- node的最大最小坐标
self.MaxY,self.MinY = 0
-- 创建cell
for index,vo in pairs(self.CellData) do
local shareCell = self:createCell(index,vo)
-- cell坐标计算
if index~=1 then
statx = statx - shareCell:getContentSize().height
end
shareCell:setPosition(0,statx)
-- 计算得出最大最小坐标
if index == 1 then
self.MinY = self.size.height - shareCell:getContentSize().height
reloadY = self.size.height - shareCell:getContentSize().height
else
self.MaxY = self.MaxY + shareCell:getContentSize().height
end
-- 根据上次的点击位置计算node的坐标位置
if index~=1 and vo._Index and self._LastCilckFIndex >= vo._Index then
reloadY = reloadY + shareCell:getContentSize().height
end
end
-- 超出限制处理
if reloadY <= self.MinY then
reloadY = self.MinY
elseif reloadY >= self.MaxY then
reloadY = self.MaxY
end
self.node:setPosition(0,reloadY)
end
-- 添加新的元素
function friend_TwoLayer:addData(Fdata,Cdata)
-- 元素数据插入
local index = #self.Fdata + 1
Fdata._Index = index
self.Fdata[index] = Fdata
self.Cdata[index] = Cdata
self.CellData[#self.CellData + 1] = clone(self.Fdata[index])
-- 创建cell
local statx = 0
for i,cell in pairs(self.node:getChildren()) do
if i~=1 then
statx = statx - cell:getContentSize().height
end
end
index = #self.CellData
local shareCell = self:createCell(index,self.CellData[index])
statx = statx - shareCell:getContentSize().height
shareCell:setPosition(0,statx)
-- 最大偏移量修改
self.MaxY = self.MaxY + shareCell:getContentSize().height
end
function friend_TwoLayer:setOnClickFCell(callBack)
self.FCallBack = callBack
end
function friend_TwoLayer:setOnClickCCell(callBack)
self.CCallBack = callBack
end
function friend_TwoLayer:onEnter()
print("friend_TwoLayer:onEnter")
end
function friend_TwoLayer:onExit()
print("friend_TwoLayer:onExit")
end
function friend_TwoLayer:onTouchBegan(touch, event)
print("friend_TwoLayer:onTouchBega1")
return true
end
function friend_TwoLayer:onTouchMoved(touch, event)
printInfo("friend_TwoLayer onTouchMoved.....")
local Y = self.node:getPositionY() + touch:getDelta().y
print("Node:getPositionY() = %d",Y)
-- 超出限制处理
if Y <= self.MinY then
return
elseif Y >= self.MaxY then
-- 可以在拉倒最大位置进行新元素的添加
return
end
self.node:setPositionY(Y)
end
function friend_TwoLayer:onTouchEnded(touch, event)
printInfo("friend_TwoLayer onTouchEnded.....")
end
return friend_TwoLayer
-- 方法二:通过tableview实现(不支持cell大小不一致)
-- 二级下拉菜单管理 TableView
friend_TwoTableView = {
}
function friend_TwoTableView:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end
function friend_TwoTableView:getInstance()
if self.instance == nil then
self.instance = self:new()
end
return self
end
function friend_TwoTableView:createTableView(size,cellSize,Fdata,Cdata)
self.FCallBack = nil
self.CCallBack = nil
self.Fdata = Fdata
self.Cdata = Cdata
for i,vo in pairs(self.Fdata) do
vo._Index = i
end
self.CellData = clone(self.Fdata)
self.cellNum = #Fdata
self.cellSize = cellSize
self.size = size
self.tableView = cc.TableView:create(cc.size(size.width, size.height))
self.tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
self.tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
self.tableView:setDelegate()
self.tableView:registerScriptHandler(handler(self, self.tableCellAtIndex), cc.TABLECELL_SIZE_AT_INDEX)
self.tableView:registerScriptHandler(handler(self, self.numberOfCellsInTableView), cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
self.tableView:registerScriptHandler(handler(self, self.cellSizeForTable), cc.TABLECELL_SIZE_FOR_INDEX)
self.tableView:reloadData()
return self.tableView
end
function friend_TwoTableView:setOnClickFCell(callBack)
self.FCallBack = callBack
end
function friend_TwoTableView:setOnClickCCell(callBack)
self.CCallBack = callBack
end
function friend_TwoTableView:tableCellAtIndex(table, idx)
local cell = table:dequeueCell()
local index = idx + 1
if nil == cell then
cell = cc.TableViewCell:new()
end
cell:removeAllChildren(true)
if not self.CellData[index].IsC then
local shareCell = ccui.ImageView:create("image/buttom/h_0001_.png")
local text = ccui.Text:create("通过微信分享"..index, Tm.Res.FontDefault, 26)
text:setPosition(shareCell:getContentSize().width/2,shareCell:getContentSize().height/2)
shareCell:addChild(text)
shareCell:setAnchorPoint(0,0)
shareCell:setTouchEnabled(true)
shareCell:setSwallowTouches(false)
shareCell:addClickEventListener(function ()
local _Index = self.CellData[index]._Index
self.CellData = {}
-- 重新计算TableView偏移
local offset = {x=0,y=0}
-- 计算显示的cell的数量
local showValue = self.size.height/self.cellSize.height
printInfo("tableview showValue=%d",showValue)
local endValue = showValue
-- 数据重置计算
if not self.LastCilckFIndex or self.LastCilckFIndex ~= _Index then
for i,vo in pairs(self.Fdata) do
self.CellData[#self.CellData + 1] = clone(vo)
if _Index == vo._Index and self.Cdata[_Index] then
for i,cvo in pairs(self.Cdata[_Index]) do
cvo.IsC = true
self.CellData[#self.CellData + 1] = clone(cvo)
end
end
end
self.LastCilckFIndex = _Index
endValue = showValue - 0.5
else
for i,vo in pairs(self.Fdata) do
self.CellData[#self.CellData + 1] = clone(vo)
end
self.LastCilckFIndex = nil
end
self.cellNum = #self.CellData
self.tableView:reloadData()
offset.y = self.tableView:minContainerOffset().y + (_Index - endValue) * self.cellSize.height
dump(offset,"tableview 偏移")
if _Index >= showValue then
self.tableView:setContentOffset(offset);
end
if self.FCallBack then
self.FCallBack(self.CellData[index])
end
end)
cell:addChild(shareCell)
else
local shareCell = ccui.ImageView:create("image/buttom/hljkhjkgho.png")
shareCell:setAnchorPoint(0,0)
cell:addChild(shareCell)
shareCell:setTouchEnabled(true)
shareCell:setSwallowTouches(false)
shareCell:addClickEventListener(function ()
if self.CCallBack then
self.CCallBack(self.CellData[index])
end
end)
end
return cell
end
function friend_TwoTableView:numberOfCellsInTableView(view)
return self.cellNum
end
function friend_TwoTableView:cellSizeForTable(table, idx)
return self.cellSize.width,self.cellSize.height
end
function friend_TwoTableView:createTableViewI(size,cellSize,Fdata,Cdata)
self.layer = display.newLayer()
self.layer:setContentSize(size.width,size.height)
self.layer:addTouchEventListener(function(sender,type)
end)
return self.layer
end
return friend_TwoTableView
|
请发表评论