The most painful part of mobile development is that you can’t release it dynamically, like you can on the Web, but Lua makes it possible. Lua is a scripting language. Using Lua to build cross-platform native applications has many advantages. For example, Lua is simple, efficient and portable.

demo

Write a simple code to demonstrate this. Create a new lua file called view.lua and place it on the sdcard of your phone. The file directory is /sdcard/view.lua

require "import"
import "android.widget.*"
import "android.content.*"

function getView(a)
    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

Copy the code

So let’s run it,

The top half of the screen is code written in Android’s XML layout. When the Run button is clicked, the Lua script loads, returns a View object, and then adds it to the layout. A simple Lua scripted view is written. Now I’m going to change it to a click event.

require "import"
import "android.widget.*"
import "android.content.*"

function getView(a)
    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(a)
        Toast.makeText(activity,"2333".0).show()
    end
    return view
end
Copy the code

Running effect

For a slightly more complex example, create a list, create a list.lua file, and place it in your phone’s sdcard/list.lua file

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(a)
    -- 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(a)
            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(a)
    local view = loadlayout(layout)
    adapter = LuaAdapter(luajava.createProxy("androlua.LuaAdapter$AdapterCreator", {
        getCount = function(a) 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 index starts at 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

Copy the code

Create listView, set Adapter, network request, refresh list. Take a look at the effect.

The code is available at github 👉 source

Schematic diagram

Wrote several articles more detailed introduction of the principle, want to know can see

  • Android and Lua
  • Explore the application of Lua in Android
  • Lua is embedded in Android

Does it support iOS?

Lua is developed in C language, portability is better, if you want to support iOS, the principle is the same, but refer to the existing cross-platform technology. Some personal views about the cross-platform, there are the cross-platform technology when it comes to features of different platforms, it is more hurt, need to separate to adaptation, there is a pile of connection to libraries, such as the selection of local images, different database platform, platform-specific API, is really a code running around eventually is a dream, a portion of code mining pit is true everywhere.

How far can Android development go?

As you can see from the schematic above, almost all of the Android SDK apis are supported.

To contact me