添加设备
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.example.defenseapplication.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.defenseapplication.R
|
||||
|
||||
data class Device(
|
||||
val name: String,
|
||||
val icon: String
|
||||
)
|
||||
class AddDeviceAdapter(private val devices: List<Device>, private val onItemClick: (Device) -> Unit) :
|
||||
RecyclerView.Adapter<AddDeviceAdapter.ViewHolder>() {
|
||||
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val ivIcon: ImageView = itemView.findViewById(R.id.ivDeviceIcon2)
|
||||
val tvName: TextView = itemView.findViewById(R.id.tvDeviceName2) // 注意:ID 要和布局中一致
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_device2, parent, false)
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val device = devices[position]
|
||||
holder.tvName.text = device.name
|
||||
// 根据 icon 字符串加载对应图片(假设 icon 是本地 drawable 的名称)
|
||||
val resId = holder.itemView.context.resources.getIdentifier(device.icon, "drawable", holder.itemView.context.packageName)
|
||||
if (resId != 0) holder.ivIcon.setImageResource(resId) else holder.ivIcon.setImageResource(R.drawable.img)
|
||||
|
||||
// 设置点击事件
|
||||
holder.itemView.setOnClickListener { onItemClick(device) }
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = devices.size
|
||||
}
|
||||
@@ -40,11 +40,11 @@ class DeviceAdapter(
|
||||
fun bind(item: DeviceUi) {
|
||||
binding.tvDeviceName.text = item.name
|
||||
if (item.isOnline) {
|
||||
binding.tvDeviceStatus.text = "\u25CF 正在运行"
|
||||
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.green))
|
||||
binding.tvDeviceName.text = "\u25CF 正在运行"
|
||||
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.green))
|
||||
} else {
|
||||
binding.tvDeviceStatus.text = "\u25CF 离线"
|
||||
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.red))
|
||||
binding.tvDeviceName.text = "\u25CF 离线"
|
||||
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.red))
|
||||
}
|
||||
binding.root.setOnClickListener {
|
||||
onItemClick(item)
|
||||
|
||||
Reference in New Issue
Block a user