Android RecyclerView 如何展示自定义列表 Kotlin

news/2024/9/6 6:10:28 标签: android, kotlin, 开发语言

Android RecyclerView 如何展示自定义列表 Kotlin

一、前提

有这么一个对象

kotlin">class DeviceDemo (val name: String, val type: String, val address: String)

要展示一个包含这个对象的列表

kotlin">bluetoothDevices.add(DeviceDemo("bb 9800", "LE", "32:34:34:23:23"))
bluetoothDevices.add(DeviceDemo("bb 9900", "RN/LE", "32:34:34:23:23"))
bluetoothDevices.add(DeviceDemo("iPhone 15 Pro", "LE", "32:34:34:23:23"))

最终效果是这样的:

在这里插入图片描述

二、定义所需要的视图 layout

1. 定义列表单元格的 layout

就是定义一个普通的 layout 视图,这个视图就是列表中每个元素的视图,自己随意定义,别忘了给每个元素添加 id。

recyclerview_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="left"
    android:layout_height="wrap_content">
    <LinearLayout
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/list_cell_bt_index"
            android:textAlignment="textEnd"
            android:layout_marginRight="10dp"
            android:text="1."
            android:textColor="@color/white"
            android:textSize="18dp"
            android:fontFamily="@font/jetbrainsmono_bold"
            android:layout_width="50dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/list_cell_bt_name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/jetbrainsmono_bold"
                android:text="BlackBerry Q10"
                android:textSize="14dp"
                android:textAlignment="center"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/list_cell_bt_type"
                android:fontFamily="@font/jetbrainsmono_bold"
                android:layout_marginLeft="10dp"
                android:textSize="12dp"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="低功耗"
                android:textColor="@color/btBlue" />
        </LinearLayout>

        <TextView
            android:layout_gravity="left"
            android:id="@+id/list_cell_bt_mac"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="34:53:E3:12:23:45"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

2. 在你的主视图 xml 中添加 RecyclerView

如下,两个地方需要注意:

  • layoutManager 就在下面这个值
  • listitem 是指的你的列表中每个单元 cell 的视图 layout,也就是上面第1步里定义的视图。
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/recyclerview_cell" />

这样就能在 xml 预览窗口中看到这个列表的样子,这样关于视图的定义就完成了。

在这里插入图片描述

三、定义对应的程序代码

列表是上面已经定义好的 DeviceDemo 类型的 List。就不再重新定义这个
要想展示这个列表,需要用到 Adapter,它的类型是 RecyclerView.Adapter

1. 定义基础元素、数据

这里只做演示用,所以定义了简单的对象:

kotlin">class DeviceDemo (val name: String, val type: String, val address: String)


// 新建一个 list 对象,演示数据
private var bluetoothDevices = mutableListOf<DeviceDemo>()

bluetoothDevices.add(DeviceDemo("bb 9800", "LE", "32:34:34:23:23"))
bluetoothDevices.add(DeviceDemo("bb 9900", "RN/LE", "32:34:34:23:23"))
bluetoothDevices.add(DeviceDemo("iPhone 15 Pro", "LE", "32:34:34:23:23"))

2. 定义 adapter

就是定义一个自己的 adapter,在里面实现 ViewHolder 方法,对上面定义的单元格视图和数据进行绑定。
需要重写 onBindViewHolder onCreateViewHolder getItemCount 几个方法变量

kotlin">package cn.kylebing.blackberry.q10_keyboard

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class BluetoothDeviceRecycleListAdapter(private val dataSet: MutableList<DeviceDemo>) :
    RecyclerView.Adapter<BluetoothDeviceRecycleListAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val index: TextView
        val deviceName: TextView
        val deviceMac: TextView
        val deviceType: TextView

        init {
            index = view.findViewById(R.id.list_cell_bt_index)
            deviceName = view.findViewById(R.id.list_cell_bt_name)
            deviceMac = view.findViewById(R.id.list_cell_bt_mac)
            deviceType = view.findViewById(R.id.list_cell_bt_type)
        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.recyclerview_cell, viewGroup, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        viewHolder.index.text = position.toString()
        viewHolder.deviceName.text = dataSet.get(position).name
        viewHolder.deviceMac.text = dataSet.get(position).address
        viewHolder.deviceType.text = dataSet.get(position).type
    }

    override fun getItemCount() = dataSet.size
}

3. 主程序中调用

kotlin">class MainActivity : AppCompatActivity() {

    private var bluetoothDevices = mutableListOf<DeviceDemo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.bluetooth_info_panel)

        bluetoothDevices.add(DeviceDemo("bb 9800", "LE", "32:34:34:23:23"))
        bluetoothDevices.add(DeviceDemo("bb 9900", "RN/LE", "32:34:34:23:23"))
        bluetoothDevices.add(DeviceDemo("iPhone 15 Pro", "LE", "32:34:34:23:23"))

        // recycleView
        val recycleView = findViewById<RecyclerView>(R.id.recyclerview)
        val adapter = BluetoothDeviceRecycleListAdapter(bluetoothDevices)
        recycleView.setAdapter(adapter)
   }
}

四、List 数据发生变化时更新界面

不可避免的,你可能需要更新原列表数据,比如删除或添加元素,此时需要界面跟着更新,就需要找到 RecyclerView 的 adapter,执行 adapter.notifyItemRangeChanged() 告知 adapter 进行更新。

kotlin">bluetoothDevices.add(DeviceDemo("new device", "LE", "32:34:34:23:23"))

findViewById<RecyclerView>(R.id.recyclerview).adapter?.notifyDataSetChanged()

五、结果

在这里插入图片描述

最终经过修改我实现的效果是这样的:

在这里插入图片描述


http://www.niftyadmin.cn/n/5392806.html

相关文章

智慧校园|智慧校园管理小程序|基于微信小程序的智慧校园管理系统设计与实现(源码+数据库+文档)

智慧校园管理小程序目录 目录 基于微信小程序的智慧校园管理系统设计与实现 一、前言 二、系统功能设计 三、系统实现 1、微信小程序前台 2、管理员后台 &#xff08;1&#xff09;学生信息管理 &#xff08;2&#xff09; 作业信息管理 &#xff08;3&#xff09;公告…

【监控】grafana图表使用快速上手

目录 1.前言 2.连接 3.图表 4.job和path 5.总结 1.前言 上一篇文章中&#xff0c;我们使用spring actuatorPrometheusgrafana实现了对一个spring boot应用的可视化监控。 【监控】Spring BootPrometheusGrafana实现可视化监控-CSDN博客 其中对grafana只是打开了一下&am…

栈和堆什么意思,Rust所有权机制又是什么

栈和堆什么意思 栈&#xff1a;存储基本数据类型和引用数据类型的指针引用(地址)&#xff0c;基本数据类型占据固定大小的内存空间。 堆&#xff1a;存储引用数据类型的值&#xff0c;引用数据类型包括对象&#xff0c;数组和函数&#xff0c;在堆中&#xff0c;引用数据类型…

Kotlin多线程

目录 线程的使用 线程的创建 例一&#xff1a;创建线程并输出Hello World Thread对象的用法 start() join() interrupt() 线程安全 原子性 可见性 有序性 线程锁 ReentrantLock ReadWriteLock 线程的使用 Java虚拟机中的多线程可以1:1映射至CPU中&#xff0c;即…

Linux之安装jdk,tomcat,mysql,部署项目

目录 一、操作流程 1.1安装jdk 1.2安装tomcat&#xff08;加创建自启动脚本&#xff09; 1.3 安装mysql 1.4部署项目 一、操作流程 首先把需要用的包放进opt文件下 1.1安装jdk 把jdk解压到/usr/local/java里 在刚刚放解压包的文件夹打开vim /etc/profile编辑器&#xff0c…

使用apt-mirror做一个本地ubuntu离线apt源

1. 安装 apt-mirror sudo apt-get install apt-mirror2. 创建文件夹 mkdir ./apt_mirror_dir3. 修改apt-mirror的配置文件 sudo gedit /etc/apt/mirror.list得到以下文件,重点对两个位置进行修改&#xff1a; ############# config ################## # ## 修改1&#xff…

一键生成请求方法的工具 —— OpenAPI Typescript Codegen

文章目录 用法自定义请求参数的方法1&#xff09;使用代码生成器提供的全局参数修改对象2&#xff09;直接定义 axios 请求库的全局参数&#xff0c;比如&#xff1a;全局请求响应拦截器 报错解决 用法 首先下载axios npm install axios官网&#xff1a;https://github.com/f…

RabbitMQ-消息队列:优先级队列、惰性队列

20、优先级队列 在我们系统中有一个订单催付的场景&#xff0c;我们的客户在天猫下的订单&#xff0c;淘宝会及时将订单推送给我们&#xff0c;如果在用户设定的时间内未付款那么就会给用户推送一条短信提醒&#xff0c;很简单的一个功能对吧。 但是&#xff0c;天猫商家对我…