68 lines
2.1 KiB
Kotlin
68 lines
2.1 KiB
Kotlin
package com.customassistant
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.ViewGroup
|
|
import android.widget.RadioButton
|
|
import android.widget.TextView
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
class ActionsAdapter(
|
|
private val actions: MutableList<ActionItem>,
|
|
private val onActionSelected: (ActionItem) -> Unit
|
|
) : RecyclerView.Adapter<ActionsAdapter.ActionViewHolder>() {
|
|
|
|
private var selectedPosition = -1
|
|
|
|
class ActionViewHolder(
|
|
private val rbAction: RadioButton,
|
|
private val tvTitle: TextView,
|
|
private val tvDescription: TextView
|
|
) : RecyclerView.ViewHolder(rbAction.parent as ViewGroup) {
|
|
|
|
fun bind(action: ActionItem, isSelected: Boolean, onSelected: () -> Unit) {
|
|
rbAction.isChecked = isSelected
|
|
tvTitle.text = action.title
|
|
tvDescription.text = action.description
|
|
|
|
itemView.setOnClickListener {
|
|
onSelected()
|
|
}
|
|
|
|
rbAction.setOnClickListener {
|
|
onSelected()
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActionViewHolder {
|
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_action, parent, false)
|
|
return ActionViewHolder(
|
|
view.findViewById(R.id.rbAction),
|
|
view.findViewById(R.id.tvActionTitle),
|
|
view.findViewById(R.id.tvActionDescription)
|
|
)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ActionViewHolder, position: Int) {
|
|
val action = actions[position]
|
|
val isSelected = position == selectedPosition
|
|
|
|
holder.bind(action, isSelected) {
|
|
val previousSelected = selectedPosition
|
|
selectedPosition = position
|
|
|
|
if (previousSelected != -1) {
|
|
notifyItemChanged(previousSelected)
|
|
}
|
|
notifyItemChanged(selectedPosition)
|
|
|
|
onActionSelected(action)
|
|
}
|
|
}
|
|
|
|
override fun getItemCount() = actions.size
|
|
|
|
fun getSelectedAction(): ActionItem? {
|
|
return if (selectedPosition != -1) actions[selectedPosition] else null
|
|
}
|
|
} |