【lua教程】Listview里的ChechBox
require "import"import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
layout={
LinearLayout,
layout_width="fill",
layout_height="fill",
orientation="vertical",
{
LinearLayout,
layout_width="fill",
layout_height="56dp",
{
Button,
text="全选",
id="btn1",
},
{
Button,
text="反选",
id="btn2",
},
{
Button,
text="打印多选数据",
id="btn3",
},
},
{
LinearLayout,
layout_width="fill",
layout_height="56dp",
{
Button,
text="删除",
id="btn4",
},
},
{
ListView,
layout_width="fill",
layout_height="fill",
id="list",
},
}
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
activity.setContentView(loadlayout(layout))
--author:OpenEyez
--功能:实现了ListView与复选框CheckBox的完美结合
--编辑器:OpenLua+_0.5.9,Androlua5.0.7
--欢迎大家加入openlua+QQ群:1015308783
item=
{
LinearLayout,
layout_width="fill",
layout_height="fill",
orientation="vertical",
{
LinearLayout,
layout_width="fill",
layout_height="100dp",
{
TextView,
layout_width="80%w",
layout_height="100dp",
gravity="center",
text="HELLOWORLD",
id="tv",
},
{
CheckBox,
layout_width="wrap_content",
layout_height="wrap_content",
focusable=false,
focusableInTouchMode=false,
clickable=false,
id="cb",
},
},
}
--数据
datas={}
--是否开启多选模式
CHOICEMODE=false
--适配器,要绑定数据
adp=LuaAdapter(activity,datas,item)
list.setAdapter(adp)
--增加一点数据进去
for i=1,2000 do
table.insert(datas,{tv="数据"..i,cb={checked=false,visibility=View.GONE}})
end
--ListView的点击事件
list.setOnItemClickListener(AdapterView.OnItemClickListener{
onItemClick=function(parent,v,pos,id)
if CHOICEMODE then
if datas["cb"]["checked"] then
datas["cb"]["checked"]=false
else
datas["cb"]["checked"]=true
end
adp.notifyDataSetChanged()
end
end
})
--ListView的长按事件
list.onItemLongClick=function(l,v,p,i)
if CHOICEMODE==false then
CHOICEMODE = true
task(1,function()
for k,v ipairs(datas) do
v["cb"]["visibility"]=View.VISIBLE
end
adp.notifyDataSetChanged()
end)
return true
else
print("已经开启了多选模式,无需重复")
return false
end
end
--全选
btn1.onClick=function()
if CHOICEMODE then
task(1,function()
for k,v ipairs(datas) do
v["cb"]["checked"]=true
end
adp.notifyDataSetChanged()
end)
else
print("未开启多选模式,长按列表开启")
end
end
--反选
btn2.onClick=function()
if CHOICEMODE then
task(1,function()
for k,v ipairs(datas) do
if v["cb"]["checked"] then
v["cb"]["checked"]=false
else
v["cb"]["checked"]=true
end
end
adp.notifyDataSetChanged()
end)
else
print("未开启多选模式,长按列表开启")
end
end
--打印
btn3.onClick=function()
if CHOICEMODE then
task(1,function()
for k,v ipairs(datas) do
if v["cb"]["checked"] then
print(v["tv"])
end
end
end)
else
print("未开启多选模式,长按列表开启")
end
end
function del_data()
for k,v in pairs(datas) do
if datas["cb"]["checked"]==true then
table.remove(datas,k)
del_data()
break
end
end
end
--删除数据
btn4.onClick=function()
if CHOICEMODE then
task(1,function()
del_data()
adp.notifyDataSetChanged()
end)
else
print("未开启多选模式,长按列表开启")
end
end
--返回键的交互
function onKeyDown(code,event)
if string.find(tostring(event),"KEYCODE_BACK") ~= nil then
if CHOICEMODE==true then
CHOICEMODE=false
task(1,function()
for k,v ipairs(datas) do
v["cb"]["checked"]=false
v["cb"]["visibility"]=View.GONE
end
adp.notifyDataSetChanged()
end)
return true
else
return false
end
end
end
页:
[1]