1+ package com.tencent.iot.explorer.link.demo.core.activity
2+
3+ import android.os.Bundle
4+ import android.view.LayoutInflater
5+ import android.view.View
6+ import android.view.ViewGroup
7+ import android.widget.ImageView
8+ import android.widget.TextView
9+ import android.widget.Toast
10+ import androidx.recyclerview.widget.LinearLayoutManager
11+ import androidx.recyclerview.widget.RecyclerView
12+ import com.tencent.iot.explorer.link.demo.BaseActivity
13+ import com.tencent.iot.explorer.link.demo.R
14+ import com.tencent.iot.explorer.link.demo.databinding.ActivityEnhancedDeviceListBinding
15+
16+ /* *
17+ * 增强版设备列表页面
18+ */
19+ class EnhancedDeviceListActivity : BaseActivity <ActivityEnhancedDeviceListBinding >() {
20+
21+ private val deviceList = mutableListOf<DeviceItem >()
22+ private lateinit var adapter: DeviceAdapter
23+
24+ override fun getViewBinding (): ActivityEnhancedDeviceListBinding =
25+ ActivityEnhancedDeviceListBinding .inflate(layoutInflater)
26+
27+ override fun initView () {
28+ // 初始化设备列表
29+ initDeviceList()
30+
31+ // 设置RecyclerView
32+ setupRecyclerView()
33+
34+ // 更新空状态显示
35+ updateEmptyState()
36+ }
37+
38+ override fun setListener () {
39+ with (binding) {
40+ // 添加设备按钮
41+ btnAddDevice.setOnClickListener {
42+ showAddDevicePage()
43+ }
44+
45+ // 空状态添加设备按钮
46+ btnAddDeviceEmpty.setOnClickListener {
47+ showAddDevicePage()
48+ }
49+
50+ // 用户信息按钮
51+ btnUserProfile.setOnClickListener {
52+ showUserProfile()
53+ }
54+ }
55+ }
56+
57+ /* *
58+ * 初始化设备列表数据
59+ */
60+ private fun initDeviceList () {
61+ // 模拟设备数据
62+ deviceList.apply {
63+ add(DeviceItem (
64+ " 小米智能摄像机 云台版2K" ,
65+ " 客厅" ,
66+ true ,
67+ R .drawable.ic_camera_device
68+ ))
69+ add(DeviceItem (
70+ " 华为智能摄像机" ,
71+ " 卧室" ,
72+ true ,
73+ R .drawable.ic_camera_device
74+ ))
75+ add(DeviceItem (
76+ " 萤石C6CN" ,
77+ " 门口" ,
78+ false ,
79+ R .drawable.ic_camera_device
80+ ))
81+ }
82+ }
83+
84+ /* *
85+ * 设置RecyclerView
86+ */
87+ private fun setupRecyclerView () {
88+ adapter = DeviceAdapter (deviceList)
89+ binding.rvDeviceList.apply {
90+ layoutManager = LinearLayoutManager (this @EnhancedDeviceListActivity)
91+ adapter = this @EnhancedDeviceListActivity.adapter
92+ }
93+
94+ // 设置设备点击事件
95+ adapter.setOnItemClickListener { position, device ->
96+ openDeviceDetail(device)
97+ }
98+
99+ // 设置菜单点击事件
100+ adapter.setOnMenuClickListener { position, device ->
101+ showDeviceMenu(position, device)
102+ }
103+ }
104+
105+ /* *
106+ * 更新空状态显示
107+ */
108+ private fun updateEmptyState () {
109+ val isEmpty = deviceList.isEmpty()
110+ binding.emptyState.visibility = if (isEmpty) View .VISIBLE else View .GONE
111+ binding.rvDeviceList.visibility = if (isEmpty) View .GONE else View .VISIBLE
112+ }
113+
114+ /* *
115+ * 显示添加设备页面
116+ */
117+ private fun showAddDevicePage () {
118+ // TODO: 跳转到添加设备页面
119+ Toast .makeText(this , " 跳转到添加设备页面" , Toast .LENGTH_SHORT ).show()
120+ jumpActivity(AddDeviceActivity ::class .java)
121+ }
122+
123+ /* *
124+ * 显示用户信息页面
125+ */
126+ private fun showUserProfile () {
127+ // TODO: 跳转到用户信息页面
128+ Toast .makeText(this , " 跳转到用户信息页面" , Toast .LENGTH_SHORT ).show()
129+ jumpActivity(PersonalInfoActivity ::class .java)
130+ }
131+
132+ /* *
133+ * 打开设备详情
134+ */
135+ private fun openDeviceDetail (device : DeviceItem ) {
136+ // TODO: 跳转到设备详情页面
137+ Toast .makeText(this , " 打开设备: ${device.name} " , Toast .LENGTH_SHORT ).show()
138+ // jumpActivity(CameraDetailActivity::class.java)
139+ }
140+
141+ /* *
142+ * 显示设备菜单
143+ */
144+ private fun showDeviceMenu (position : Int , device : DeviceItem ) {
145+ // TODO: 显示设备操作菜单(设置、分享、删除等)
146+ Toast .makeText(this , " 显示设备菜单: ${device.name} " , Toast .LENGTH_SHORT ).show()
147+ }
148+
149+ /* *
150+ * 刷新设备列表
151+ */
152+ private fun refreshDeviceList () {
153+ adapter.notifyDataSetChanged()
154+ updateEmptyState()
155+ }
156+
157+ override fun onResume () {
158+ super .onResume()
159+ // 页面显示时刷新设备列表
160+ refreshDeviceList()
161+ }
162+ }
163+
164+ /* *
165+ * 设备数据类
166+ */
167+ data class DeviceItem (
168+ val name : String ,
169+ val location : String ,
170+ val isOnline : Boolean ,
171+ val iconRes : Int
172+ )
173+
174+ /* *
175+ * 设备列表适配器
176+ */
177+ class DeviceAdapter (private val deviceList : List <DeviceItem >) :
178+ RecyclerView .Adapter <DeviceAdapter .DeviceViewHolder >() {
179+
180+ private var itemClickListener: ((Int , DeviceItem ) -> Unit )? = null
181+ private var menuClickListener: ((Int , DeviceItem ) -> Unit )? = null
182+
183+ class DeviceViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView) {
184+ val ivDeviceIcon: ImageView = itemView.findViewById(R .id.iv_device_icon)
185+ val tvDeviceName: TextView = itemView.findViewById(R .id.tv_device_name)
186+ val tvDeviceLocation: TextView = itemView.findViewById(R .id.tv_device_location)
187+ val tvDeviceStatus: TextView = itemView.findViewById(R .id.tv_device_status)
188+ val indicatorStatus: View = itemView.findViewById(R .id.indicator_status)
189+ val btnDeviceMenu: ImageView = itemView.findViewById(R .id.btn_device_menu)
190+ }
191+
192+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): DeviceViewHolder {
193+ val view = LayoutInflater .from(parent.context)
194+ .inflate(R .layout.item_device_card, parent, false )
195+ return DeviceViewHolder (view)
196+ }
197+
198+ override fun onBindViewHolder (holder : DeviceViewHolder , position : Int ) {
199+ val device = deviceList[position]
200+
201+ holder.tvDeviceName.text = device.name
202+ holder.tvDeviceLocation.text = device.location
203+ holder.ivDeviceIcon.setImageResource(device.iconRes)
204+
205+ // 设置设备状态
206+ if (device.isOnline) {
207+ holder.tvDeviceStatus.text = " 在线"
208+ holder.indicatorStatus.setBackgroundResource(R .drawable.bg_status_online)
209+ } else {
210+ holder.tvDeviceStatus.text = " 离线"
211+ // holder.indicatorStatus.setBackgroundResource(R.drawable.bg_status_offline)
212+ }
213+
214+ // 设备点击事件
215+ holder.itemView.setOnClickListener {
216+ itemClickListener?.invoke(position, device)
217+ }
218+
219+ // 菜单按钮点击事件
220+ holder.btnDeviceMenu.setOnClickListener {
221+ menuClickListener?.invoke(position, device)
222+ }
223+ }
224+
225+ override fun getItemCount (): Int = deviceList.size
226+
227+ fun setOnItemClickListener (listener : (Int , DeviceItem ) -> Unit ) {
228+ itemClickListener = listener
229+ }
230+
231+ fun setOnMenuClickListener (listener : (Int , DeviceItem ) -> Unit ) {
232+ menuClickListener = listener
233+ }
234+ }
0 commit comments