扫码附件设备,链接设备动画
This commit is contained in:
@@ -10,14 +10,33 @@ import com.example.defenseapplication.R
|
||||
|
||||
data class Device(
|
||||
val name: String,
|
||||
val icon: String
|
||||
val icon: String // 暂时保留,不再实际使用静态图片
|
||||
)
|
||||
class AddDeviceAdapter(private val devices: List<Device>, private val onItemClick: (Device) -> Unit) :
|
||||
RecyclerView.Adapter<AddDeviceAdapter.ViewHolder>() {
|
||||
|
||||
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 要和布局中一致
|
||||
val tvName: TextView = itemView.findViewById(R.id.tvDeviceName2)
|
||||
|
||||
// 启动帧动画
|
||||
fun startAnimation() {
|
||||
val drawable = ivIcon.drawable
|
||||
if (drawable is android.graphics.drawable.AnimationDrawable && !drawable.isRunning) {
|
||||
drawable.start()
|
||||
}
|
||||
}
|
||||
|
||||
// 停止帧动画
|
||||
fun stopAnimation() {
|
||||
val drawable = ivIcon.drawable
|
||||
if (drawable is android.graphics.drawable.AnimationDrawable && drawable.isRunning) {
|
||||
drawable.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
@@ -28,13 +47,17 @@ class AddDeviceAdapter(private val devices: List<Device>, private val onItemClic
|
||||
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.ivIcon.setImageResource(R.drawable.img)
|
||||
|
||||
// 设置点击事件
|
||||
// 点击事件
|
||||
holder.itemView.setOnClickListener { onItemClick(device) }
|
||||
}
|
||||
|
||||
override fun onViewRecycled(holder: ViewHolder) {
|
||||
super.onViewRecycled(holder)
|
||||
// 视图被回收时停止动画,节省资源
|
||||
holder.stopAnimation()
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = devices.size
|
||||
}
|
||||
Reference in New Issue
Block a user