I have written an adapter for RecyclerView
. It can handle onClicks for RecyclerView
items and select the color name and edit TextView
for the clicked item. By default the first item TextView name is the selected color.
When a user clicks another item the last item TextView
is named black. When the the user clicks a color, such as granadier, then the TextView
is changed to that color's name. And so on.
Can my code quality and code style be improved? Additionally I'm not a fan of, when a user clicks on new item, calling notifyDataSetChanged
on all items.
AudioRecordsListAdapter.kt
class AudioRecordsListAdapter :
RecyclerView.Adapter<AudioRecordsListAdapter.AudioRecordViewHolder>() {
var items: MutableList<AudioRecordUI> = ArrayList()
set(value) {
if (value.size > 0) {
value.first().isActive = true
}
items.clear()
items.addAll(value)
notifyDataSetChanged()
}
var isSelectableMode = false
var onAudioRecordClickListener: OnAudioRecordClickListener? = null
companion object {
var dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")
var timeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("mm::ss")
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AudioRecordViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.sound_record_item, parent, false)
return AudioRecordViewHolder(view)
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: AudioRecordViewHolder, position: Int) {
holder.bind(items[position])
}
inner class AudioRecordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private var audioRecordUI: AudioRecordUI? = null
fun bind(audioRecordUI: AudioRecordUI) {
this.audioRecordUI = audioRecordUI
val size = formatFileSize(itemView.context, audioRecordUI.getSize())
itemView.name.text = audioRecordUI.getName()
itemView.date.text = audioRecordUI.getDate().format(dateFormatter)
itemView.time.text = audioRecordUI.getTime().format(timeFormatter)
itemView.size.text = size
itemView.select.isChecked = audioRecordUI.isSelect
if (audioRecordUI.isActive)
itemView.name.setTextColor(itemView.context.getColor(R.color.grenadier))
else
itemView.name.setTextColor(itemView.context.getColor(R.color.black))
setClickListener(audioRecordUI)
setLongClickListener()
}
private fun setClickListener(audioRecorderUI: AudioRecordUI) {
itemView.setOnClickListener {
if (isSelectableMode) {
itemView.select.isChecked = !itemView.select.isChecked
} else {
onAudioRecordClickListener?.onClick(audioRecorderUI.audioRecorderEmpty)
clearActive()
audioRecorderUI.isActive = true
notifyDataSetChanged()
}
}
}
private fun clearActive() {
items.forEach { it.isActive = false }
}
private fun setLongClickListener() {
itemView.setOnLongClickListener {
isSelectableMode = !isSelectableMode
if (!isSelectableMode) {
clearSelect()
}
itemView.select.isChecked = !itemView.select.isChecked
true
}
}
private fun clearSelect() {
items.forEach { it.isSelect = false }
notifyDataSetChanged()
}
}
interface OnAudioRecordClickListener {
fun onClick(audioRecordEmpty: AudioRecordEmpty)
}
}