首页功能交互

This commit is contained in:
2026-04-28 11:06:51 +08:00
parent 8a5f599266
commit 805c9ef068
8 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
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 isOnline: Boolean
)
class DeviceAdapter : 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.tvDeviceName.text = item.name
if (item.isOnline) {
binding.tvDeviceStatus.text = "\u25CF 正在运行"
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.green))
} else {
binding.tvDeviceStatus.text = "\u25CF 离线"
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.red))
}
}
}
}