开源软件名称(OpenSource Name):hanks-zyh/luaDevAndroid开源软件地址(OpenSource Url):https://github.com/hanks-zyh/luaDevAndroid开源编程语言(OpenSource Language):Java 89.5%开源软件介绍(OpenSource Introduction):做移动端开发,做蛋疼的就是不能动态发版,不能像 web 那样发版立即全部用户生效,然而 lua语言 为其提供了可能性。使用 lua 来构建跨平台原生应用有许多好处,比如 lua 语言简洁高效,可移植性好, Lua虚拟机极为轻量,仅占用200到300k的内存空间,且速度极快。 演示写一个简单的代码演示一下。新建一个 lua 文件,叫做 require "import"
import "android.widget.*"
import "android.content.*"
function getView()
local layout = {
LinearLayout,
orientation = "vertical",
layout_width = "fill",
layout_height = "fill",
{
Button,
id = "btn",
layout_marginTop="8dp",
layout_width = "fill",
layout_height = "50dp",
text = "click"
},
}
local view = loadlayout(layout)
return view
end
运行一下, 屏幕中上半部分是 Android 的 xml 布局中写好的代码,当点击运行按钮时,加载 lua 脚本,返回一个 View 对象,然后添加到布局中。一个简单的 lua 脚本编写的视图就写好了。 接下来修改一下,设置个点击事件。 require "import"
import "android.widget.*"
import "android.content.*"
function getView()
local layout = {
LinearLayout,
orientation = "vertical",
layout_width = "fill",
layout_height = "fill",
{
Button,
id = "btn",
layout_marginTop="8dp",
layout_width = "fill",
layout_height = "50dp",
text = "click"
},
}
local ids = {} -- store ids to find view
local view = loadlayout(layout, ids)
ids.btn.onClick = function()
Toast.makeText(activity,"2333",0).show()
end
return view
end 运行效果 再来个稍微复杂点的例子,写个列表,新建 require "import"
import "android.widget.*"
import "android.content.*"
import "android.view.View"
import "androlua.LuaHttp"
import "androlua.LuaAdapter"
import "androlua.LuaImageLoader"
local JSON = require("cjson")
local uihelper = require('uihelper')
-- create view table
local layout = {
LinearLayout,
orientation = "vertical",
layout_width = "fill",
layout_height = "fill",
{
ListView,
id = "listview",
dividerHeight = 0,
layout_width = "fill",
layout_height = "fill",
},
}
local item_view = {
FrameLayout,
layout_width = "fill",
layout_height = "240dp",
{
ImageView,
id = "iv_image",
layout_width = "fill",
layout_height = "fill",
scaleType = "centerCrop",
},
{
TextView,
id = "tv_title",
background = "#66000000",
layout_width = "fill",
layout_height = "fill",
padding = "32dp",
gravity = "center",
maxLines = "5",
lineSpacingMultiplier = '1.2',
textSize = "14sp",
textColor = "#CCFFFFFF",
},
}
local data = {
dailyList = {}
}
local adapter
local function getData()
-- http://baobab.kaiyanapp.com/api/v1/feed
local url = data.nextPageUrl
if url == nil then url = 'http://baobab.kaiyanapp.com/api/v1/feed?udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04' end
if url:find('udid=') == nil then url = url .. '&udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04' end
print(url)
LuaHttp.request({ url = url }, function(error, code, body)
if error or code ~= 200 then
print('fetch data error')
return
end
local str = JSON.decode(body)
uihelper.runOnUiThread(activity, function()
data.nextPageUrl = str.nextPageUrl
local list = str.dailyList[1].videoList
for i = 1, #list do
data.dailyList[#data.dailyList + 1] = list[i]
end
adapter.notifyDataSetChanged()
end)
end)
end
local function launchDetail(item)
Toast.makeText(activity, item.title, 0).show()
end
function getView()
local view = loadlayout(layout)
adapter = LuaAdapter(luajava.createProxy("androlua.LuaAdapter$AdapterCreator", {
getCount = function() return #data.dailyList end,
getItem = function(position) return nil end,
getItemId = function(position) return position end,
getView = function(position, convertView, parent)
position = position + 1 -- lua 索引从 1开始
if position == #data.dailyList then
getData()
end
if convertView == nil then
local views = {} -- store views
convertView = loadlayout(item_view, views, ListView)
if parent then
local params = convertView.getLayoutParams()
params.width = parent.getWidth()
end
convertView.setTag(views)
end
local views = convertView.getTag()
local item = data.dailyList[position]
if item then
LuaImageLoader.load(views.iv_image, item.coverForFeed)
views.tv_title.setText(item.title)
end
return convertView
end
}))
listview.setAdapter(adapter)
listview.setOnItemClickListener(luajava.createProxy("android.widget.AdapterView$OnItemClickListener", {
onItemClick = function(adapter, view, position, id)
launchDetail(data.dailyList[position + 1])
end,
}))
getData()
return view
end
创建 listView , 设置 adapter ,网络请求,刷新列表。看下效果吧。 代码放到了 github |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论