一、数据:
使用一个树管理红点的关系,每个节点中的数据结构如下:
treenod:{
_data:红点中的相关数据
parent:父节点
childlist:子节点列表
}
_data:{
name:用于识别同层级的节点
type:红点类型
isshow:红点显示标志位
checkreddotfun:更新红点函数,返回值true/false
handlereddotfun:显示红点时触发的函数
}
红点类型:
reddottype = {
none = 0,–无状态
onlyreddot = 1,–只显示红点
numreddot = 2,–存在数据的红点
}
二、具体代码
local reddottype = reddottype
local reddotdata = {
}
reddotdata.treedic = {
}
reddotdata.newreddotdata = function(name, type, checkreddotfun, handlereddotfun)
local o = {
name = name,
type = type or reddottype.none,
num = 0,
isshow = false,
checkreddotfun = checkreddotfun,
handlereddotfun = handlereddotfun,
}
return o
end
local treenode = {
}
treenode.__index = treenode
function treenode:new(data)
local o = {
childlist = {
}, parent = nil, _data = data}
setmetatable(o, treenode)
return o
end
function treenode:addchild(name, checktype, checkreddotfun, handlereddotfun)
local data = reddotdata.newreddotdata(name, checktype, checkreddotfun, handlereddotfun)
local node = treenode(data)
node.parent = self
node.index = #self.childlist + 1
self.childlist[#self.childlist + 1] = node
--添加的时候自动检测一次
if checkreddotfun then
self:check()
end
return node
end
--nodepath: 节点路径(不包含self节点的名称)
function treenode:findchild(nodepath)
log.d(nodepath)
nodepath = utils.string.split(nodepath, "[.]")
local node = self
local isfindchild = false
local isfinishfind = false
for i = 1, #nodepath do
local name = nodepath[i]
isfindchild = false
for _, child in ipairs(node.childlist) do
if child._data.name == name then
node = child
isfindchild = true
break
end
end
--如果没找到当前的子节点,则不成功
if not isfindchild then
break
end
--如果全部节点都找到,则查找成功
if i == #nodepath then
isfinishfind = true
end
end