Я пробовал это, но не могу найти идеальное решение.

Вот мой адаптер< /strong>
Код: Выделить всё
class ChatAdapter(
var context: Context,
private var chatList: ArrayList,
private var textToSpeech: TextToSpeech,
private var copyClick: (Int) -> Unit,
private var forwardClick: (Int) -> Unit,
private var speechClick: (Int, ViewHolder) -> Unit,
private var pauseClick: (ViewHolder) -> Unit
Код: Выделить всё
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var message: AppCompatTextView = view.findViewById(R.id.tvMessage)
var forward = view.findViewById(R.id.ivForward)
var ivPlay = view.findViewById(R.id.ivPlay)
var ivPause = view.findViewById(R.id.ivPause)
var copy = view.findViewById(R.id.ivCopy)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return if (chatList[viewType].isSend!!) {
ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.item_chat_send_message, parent, false)
)
} else {
ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.item_chat_receive_message, parent, false)
)
}
}
override fun getItemCount(): Int {
return chatList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (chatList[position].isSend!!) {
holder.message.text = chatList[position].text
} else {
holder.message.text = chatList[position].text?.trim()
holder.copy.setOnClickListener {
copyClick.invoke(position)
}
holder.ivPlay.setOnClickListener {
speechClick.invoke(position, holder)
}
holder.ivPause.setOnClickListener {
pauseClick.invoke(holder)
}
holder.forward.setOnClickListener {
forwardClick.invoke(position)
}
}
}
override fun getItemViewType(position: Int): Int {
return position
}
Код: Выделить всё
fun setData() {
textToSpeech = TextToSpeech(activity) {}
textToSpeech.language = Locale.US
chatList = ArrayList()
val model = (ChoicesItem(
activity.getString(R.string.stop),
0,
activity.getString(com.hashmob.aichat.R.string.hi_there),
false,
null
))
chatList.add(model)
adapter = ChatAdapter(
activity,
chatList, textToSpeech,
this::onCopyClick,
this::onForwardClick,
this::onSpeechClick,
this::onPauseClick
)
binding.rcvChat.layoutManager = LinearLayoutManager(activity)
binding.rcvChat.setHasFixedSize(true)
binding.rcvChat.adapter = adapter
adapter.notifyDataSetChanged()
}

Подробнее здесь: https://stackoverflow.com/questions/784 ... -stop-in-r