首页功能交互
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user