74 lines
2.5 KiB
Kotlin
74 lines
2.5 KiB
Kotlin
package com.example.defenseapplication.adapter
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.ViewGroup
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.example.defenseapplication.R
|
|
import com.example.defenseapplication.databinding.ItemDeviceBinding
|
|
|
|
data class DeviceUi(
|
|
val name: String,
|
|
val status: String,
|
|
val deviceId: String = ""
|
|
) {
|
|
val isOnline: Boolean
|
|
get() = status == "1"
|
|
}
|
|
|
|
class DeviceAdapter(
|
|
private val onItemClick: (DeviceUi) -> Unit = {}
|
|
) : RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder>() {
|
|
|
|
private val items = mutableListOf<DeviceUi>()
|
|
|
|
fun submitList(deviceList: List<DeviceUi>) {
|
|
items.clear()
|
|
items.addAll(deviceList)
|
|
notifyDataSetChanged()
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceViewHolder {
|
|
val binding = ItemDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
|
return DeviceViewHolder(binding)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
|
|
holder.bind(items[position])
|
|
}
|
|
|
|
override fun getItemCount(): Int = items.size
|
|
|
|
inner class DeviceViewHolder(private val binding: ItemDeviceBinding) :
|
|
RecyclerView.ViewHolder(binding.root) {
|
|
|
|
fun bind(item: DeviceUi) {
|
|
binding.tvDeviceType.text = item.name
|
|
when (item.status) {
|
|
"1" -> {
|
|
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.green))
|
|
binding.tvDeviceName.text = "\u25CF 在线"
|
|
}
|
|
"0" -> {
|
|
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.red))
|
|
binding.tvDeviceName.text = "\u25CF 离线"
|
|
}
|
|
"2" -> {
|
|
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.orange))
|
|
binding.tvDeviceName.text = "\u25CF 故障"
|
|
}
|
|
"3" -> {
|
|
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.gray))
|
|
binding.tvDeviceName.text = "\u25CF 到期"
|
|
}
|
|
else -> {
|
|
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.gray))
|
|
binding.tvDeviceName.text = "\u25CF 未知"
|
|
}
|
|
}
|
|
binding.root.setOnClickListener {
|
|
onItemClick(item)
|
|
}
|
|
}
|
|
}
|
|
}
|