Fork me on GitHub

【Kotlin】 列表适配器

列表的显示和操作

定义操作接口,只定义编辑和删除的功能,其他的功能可以类似扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 列表中项的操作
 */
interface ItemOperatorAdapter<T> {
    /**
     * 删除
     */
    fun onDelete(t: T)
 
    /**
     * 编辑
     */
    fun onEdit(t: T)
}

  定义RecyclerView列表适配器 

      定义页面的控件

1
2
3
4
5
<androidx.recyclerview.widget.RecyclerView
     android:id="@+id/rv_Content_setting_account"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/app_white" />

  定义列表适配器

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
class AccoutListItemAdapter(
    private val data: List<AccountEntity>,
    private val operator: ItemOperatorAdapter<AccountEntity>
) :
    RecyclerView.Adapter<AccoutListItemAdapter.ViewHolder>() {
 
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
 
        return ViewHolder(
            RvAccoutContentItemBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ), parent, false
            )
        )
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(data[position], operator)
    }
 
    class ViewHolder(private val binding: RvAccoutContentItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: AccountEntity, operator: ItemOperatorAdapter<AccountEntity>) {
           
            binding.tvPauseNumber.text = "67"
 
        }
    }
 
    override fun getItemCount(): Int {
        return data.size
    }
 
 
}

  

mvvm绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
val inventoryListItemAdapter = AccoutListItemAdapter(testList,
                object : ItemOperatorAdapter<AccountEntity> {
                    override fun onDelete(t: AccountEntity) {
                        val customDialog = CustomDialog(requireActivity())
                        customDialog.setsTitle("温馨提示").setsMessage("是否删除(${t.nick_name})账号?")
                            .setsCancel("取消", View.OnClickListener {
                                customDialog.dismiss()
                            }).setsConfirm("确定", View.OnClickListener {
                                viewModel.delete(t)//删除用户
                                customDialog.dismiss()
                            }).show()
 
                    }
 
                    override fun onEdit(t: AccountEntity) {
                        showEditAccountPopWindow(t)
                    }
                })
            binding.rvContentSettingAccount.adapter = inventoryListItemAdapter

  

 

posted @   黄高林  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示