Created
May 6, 2020 12:41
-
-
Save samiuelson/e708a39d18a729a8232c551f8ccf4274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.getstream.livestreamclone | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ListAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
import io.getstream.chat.android.client.models.Message | |
import kotlinx.android.synthetic.main.item_message.view.* | |
class MessagesListAdapter() : ListAdapter<Message, MessageViewHolder>(DiffCallback) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder { | |
val itemView = LayoutInflater.from(parent.context) | |
.inflate(R.layout.item_message, parent, false) | |
return MessageViewHolder(itemView) | |
} | |
override fun onBindViewHolder(holder: MessageViewHolder, position: Int) { | |
holder.bindMessage(getItem(position)) | |
} | |
companion object { | |
val DiffCallback = object : DiffUtil.ItemCallback<Message>() { | |
override fun areItemsTheSame(oldItem: Message, newItem: Message): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: Message, newItem: Message): Boolean { | |
return oldItem == newItem | |
} | |
} | |
} | |
} | |
class MessageViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
fun bindMessage(message: Message) { | |
message.apply { | |
itemView.avatar.loadUrl(user.image, R.drawable.ic_person_white_24dp) | |
itemView.userName.text = user.name | |
itemView.message.text = text | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment