Lua适配器

  1. SimpleAdapter (简单适配器)

    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
    require "import"
    import "android.widget.*"
    import "android.view.*"

    --创建数据
    data = {
    {
    title = "标题1",
    subtitle = "副标题1",
    icon = "👍"
    },
    {
    title = "标题2",
    subtitle = "副标题2",
    icon = "🎮"
    }
    }

    --创建列表项布局
    item = {
    LinearLayout;
    orientation = "horizontal";
    layout_width = "match_parent";
    layout_height = "wrap_content";
    padding = "16dp";
    {
    TextView;
    id = "icon";
    textSize = "24sp";
    layout_marginRight = "16dp";
    };
    {
    LinearLayout;
    orientation = "vertical";
    layout_width = "match_parent";
    layout_height = "wrap_content";
    {
    TextView;
    id = "title";
    textSize = "16sp";
    textColor = "#000000";
    };
    {
    TextView;
    id = "subtitle";
    textSize = "14sp";
    textColor = "#666666";
    };
    };
    };

    --创建适配器
    adapter = SimpleAdapter(activity, data, item,
    {"icon", "title", "subtitle"},
    {id.icon, id.title, id.subtitle})

    --设置适配器
    list = ListView(activity)
    list.adapter = adapter
    activity.setContentView(list)
  2. ArrayAdapter (数组适配器)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    require "import"
    import "android.widget.*"

    --创建数据
    data = {"项目1", "项目2", "项目3", "项目4"}

    --创建适配器
    adapter = ArrayAdapter(activity,
    android.R.layout.simple_list_item_1,
    data)

    --设置适配器
    list = ListView(activity)
    list.adapter = adapter
    activity.setContentView(list)
  3. LuaAdapter (Lua适配器)

    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
    require "import"
    import "android.widget.*"

    --创建列表项布局
    item = {
    LinearLayout;
    layout_width = "match_parent";
    layout_height = "wrap_content";
    padding = "16dp";
    {
    TextView;
    id = "text";
    textSize = "16sp";
    textColor = "#000000";
    };
    };

    --创建数据
    data = {
    {text = "项目1"},
    {text = "项目2"},
    {text = "项目3"}
    }

    --创建适配器
    adapter = LuaAdapter(activity, data, item)

    --设置适配器
    list = ListView(activity)
    list.adapter = adapter
    activity.setContentView(list)

    --动态添加数据
    function addItem()
    table.insert(data, {text = "新项目"})
    adapter.notifyDataSetChanged()
    end
  4. BaseAdapter (基础适配器)

    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
    require "import"
    import "android.widget.*"
    import "android.view.*"

    --创建自定义适配器
    MyAdapter = {
    data = {};
    getCount = function()
    return #this.data
    end;
    getItem = function(position)
    return this.data[position + 1]
    end;
    getItemId = function(position)
    return position
    end;
    getView = function(position, convertView, parent)
    local holder
    if convertView == nil then
    convertView = loadlayout({
    LinearLayout;
    layout_width = "match_parent";
    layout_height = "wrap_content";
    padding = "16dp";
    {
    TextView;
    id = "text";
    textSize = "16sp";
    };
    })
    holder = {
    text = convertView.findViewById(id.text)
    }
    convertView.tag = holder
    else
    holder = convertView.tag
    end

    local item = this.getItem(position)
    holder.text.text = item

    return convertView
    end;
    }

    --创建数据
    data = {"自定义1", "自定义2", "自定义3"}

    --创建适配器实例
    adapter = BaseAdapter(MyAdapter)
    adapter.data = data

    --设置适配器
    list = ListView(activity)
    list.adapter = adapter
    activity.setContentView(list)
  5. GridAdapter (网格适配器)

    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
    require "import"
    import "android.widget.*"

    --创建列表项布局
    item = {
    CardView;
    layout_width = "match_parent";
    layout_height = "wrap_content";
    radius = "8dp";
    layout_margin = "4dp";
    {
    TextView;
    id = "text";
    layout_width = "match_parent";
    layout_height = "100dp";
    gravity = "center";
    textSize = "16sp";
    };
    };

    --创建数据
    data = {}
    for i = 1, 20 do
    table.insert(data, {text = "格子"..i})
    end

    --创建适配器
    adapter = LuaAdapter(activity, data, item)

    --创建网格视图
    grid = GridView(activity)
    grid.numColumns = 3
    grid.adapter = adapter
    activity.setContentView(grid)
  6. RecyclerAdapter (回收视图适配器)

    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
    require "import"
    import "android.widget.*"
    import "androidx.recyclerview.widget.*"

    --创建列表项布局
    item = {
    CardView;
    layout_width = "match_parent";
    layout_height = "wrap_content";
    layout_margin = "8dp";
    elevation = "2dp";
    radius = "4dp";
    {
    TextView;
    id = "text";
    layout_width = "match_parent";
    layout_height = "wrap_content";
    padding = "16dp";
    textSize = "16sp";
    };
    };

    --创建数据
    data = {}
    for i = 1, 100 do
    table.insert(data, {text = "项目"..i})
    end

    --创建适配器
    adapter = LuaRecyclerAdapter(activity, data, item)

    --创建RecyclerView
    recycler = RecyclerView(activity)
    recycler.layoutManager = LinearLayoutManager(activity)
    recycler.adapter = adapter
    activity.setContentView(recycler)

每个适配器的特点:

  1. SimpleAdapter:
  • 适合显示复杂的列表项
  • 可以同时绑定多个视图
  • 数据结构使用表格式
  1. ArrayAdapter:
  • 最简单的适配器
  • 适合显示单一文本列表
  • 数据结构使用数组
  1. LuaAdapter:
  • AndroLua+特有的适配器
  • 使用Lua表来描述布局
  • 方便动态更新数据
  1. BaseAdapter:
  • 完全自定义的适配器
  • 可以实现最复杂的列表项
  • 需要自己处理视图重用
  1. GridAdapter:
  • 用于网格布局
  • 通常配合GridView使用
  • 可以显示图片网格等
  1. RecyclerAdapter:
  • 性能最好的适配器
  • 强制实现视图重用
  • 支持多种布局管理器

使用建议:

  1. 简单列表用ArrayAdapter
  2. 常规列表用LuaAdapter
  3. 复杂列表用SimpleAdapter
  4. 特殊需求用BaseAdapter
  5. 网格布局用GridAdapter
  6. 大量数据用RecyclerAdapter

是的,还有一些其他类型的适配器,我继续补充:

  1. CursorAdapter (数据库游标适配器)

    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
    require "import"
    import "android.widget.*"
    import "android.database.sqlite.*"
    import "android.content.*"

    --创建或打开数据库
    db = SQLiteDatabase.openOrCreateDatabase(
    activity.getDatabasePath("test.db"),
    nil)

    --创建表
    db.execSQL([[
    CREATE TABLE IF NOT EXISTS test_table (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT,
    content TEXT
    )]])

    --插入测试数据
    db.execSQL("INSERT INTO test_table (title,content) VALUES (?,?)",
    {"标题1","内容1"})
    db.execSQL("INSERT INTO test_table (title,content) VALUES (?,?)",
    {"标题2","内容2"})

    --查询数据
    cursor = db.rawQuery("SELECT * FROM test_table", nil)

    --创建适配器
    adapter = SimpleCursorAdapter(activity,
    android.R.layout.simple_list_item_2,
    cursor,
    {"title","content"},
    {android.R.id.text1,android.R.id.text2},
    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER)

    --设置适配器
    list = ListView(activity)
    list.adapter = adapter
    activity.setContentView(list)
  2. FragmentPagerAdapter (页面适配器)

    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
    require "import"
    import "android.widget.*"
    import "androidx.viewpager.*"
    import "androidx.fragment.app.*"

    --创建Fragment
    function createFragment(text)
    return luajava.override(Fragment,{
    onCreateView=function(inflater,container,savedInstanceState)
    local view = TextView(activity)
    view.text = text
    view.gravity = Gravity.CENTER
    return view
    end
    })
    end

    --创建适配器
    adapter = luajava.override(FragmentPagerAdapter,{
    getCount=function()
    return 3
    end,
    getItem=function(position)
    return createFragment("页面 "..(position+1))
    end,
    getPageTitle=function(position)
    return "标题 "..(position+1)
    end
    })

    --创建ViewPager
    pager = ViewPager(activity)
    pager.adapter = adapter

    --设置布局
    activity.setContentView(pager)
  3. SpinnerAdapter (下拉列表适配器)

    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
    require "import"
    import "android.widget.*"

    --创建数据
    data = {"选项1", "选项2", "选项3", "选项4"}

    --创建适配器
    adapter = ArrayAdapter(activity,
    android.R.layout.simple_spinner_item,
    data)

    --设置下拉样式
    adapter.setDropDownViewResource(
    android.R.layout.simple_spinner_dropdown_item)

    --创建Spinner
    spinner = Spinner(activity)
    spinner.adapter = adapter

    --选择监听
    spinner.onItemSelected = function(parent, view, position, id)
    print("选择了: "..data[position+1])
    end

    --设置布局
    layout = LinearLayout(activity)
    layout.addView(spinner)
    activity.setContentView(layout)
  4. ExpandableListAdapter (可展开列表适配器)

    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
    require "import"
    import "android.widget.*"

    --创建组数据
    groups = {"组1", "组2", "组3"}

    --创建子项数据
    children = {
    {"组1项1", "组1项2", "组1项3"},
    {"组2项1", "组2项2"},
    {"组3项1", "组3项2", "组3项3", "组3项4"}
    }

    --创建适配器
    adapter = BaseExpandableListAdapter {
    getGroupCount = function()
    return #groups
    end,

    getChildrenCount = function(groupPosition)
    return #children[groupPosition + 1]
    end,

    getGroup = function(groupPosition)
    return groups[groupPosition + 1]
    end,

    getChild = function(groupPosition, childPosition)
    return children[groupPosition + 1][childPosition + 1]
    end,

    getGroupId = function(groupPosition)
    return groupPosition
    end,

    getChildId = function(groupPosition, childPosition)
    return childPosition
    end,

    hasStableIds = function()
    return true
    end,

    getGroupView = function(groupPosition, isExpanded, convertView, parent)
    local view = TextView(activity)
    view.textSize = 20
    view.padding = "16dp"
    view.text = getGroup(groupPosition)
    return view
    end,

    getChildView = function(groupPosition, childPosition, isLastChild, convertView, parent)
    local view = TextView(activity)
    view.textSize = 16
    view.padding = "32dp"
    view.text = getChild(groupPosition, childPosition)
    return view
    end,

    isChildSelectable = function(groupPosition, childPosition)
    return true
    end
    }

    --创建可展开列表
    list = ExpandableListView(activity)
    list.adapter = adapter

    --点击事件
    list.onChildClick = function(parent, v, groupPosition, childPosition, id)
    print("点击了:"..children[groupPosition + 1][childPosition + 1])
    return true
    end

    --设置布局
    activity.setContentView(list)
  5. PagerAdapter (自定义页面适配器)

    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
    require "import"
    import "android.widget.*"
    import "androidx.viewpager.*"

    --创建视图数据
    views = {}
    for i = 1, 5 do
    local view = TextView(activity)
    view.text = "页面 "..i
    view.textSize = 30
    view.gravity = Gravity.CENTER
    table.insert(views, view)
    end

    --创建适配器
    adapter = PagerAdapter {
    getCount = function()
    return #views
    end,

    isViewFromObject = function(view, object)
    return view == object
    end,

    instantiateItem = function(container, position)
    local view = views[position + 1]
    container.addView(view)
    return view
    end,

    destroyItem = function(container, position, object)
    container.removeView(object)
    end
    }

    --创建ViewPager
    pager = ViewPager(activity)
    pager.adapter = adapter

    --设置布局
    activity.setContentView(pager)

每个适配器的特点:

  1. CursorAdapter:
  • 用于数据库查询结果显示
  • 自动管理数据库游标
  • 适合大量数据
  1. FragmentPagerAdapter:
  • 用于Fragment页面切换
  • 保持Fragment状态
  • 适合固定数量的页面
  1. SpinnerAdapter:
  • 用于下拉选择框
  • 支持自定义下拉样式
  • 适合选项列表
  1. ExpandableListAdapter:
  • 用于两级列表显示
  • 支持展开收起
  • 适合分类数据显示
  1. PagerAdapter:
  • 用于自定义页面切换
  • 灵活的视图管理
  • 适合动态页面

使用建议:

  1. 数据库相关用CursorAdapter
  2. 页面切换用FragmentPagerAdapter或PagerAdapter
  3. 下拉选择用SpinnerAdapter
  4. 分类列表用ExpandableListAdapter