1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| 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')
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() 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 if position == #data.dailyList then getData() end if convertView == nil then local 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
|