Created
November 13, 2020 18:10
-
-
Save samiuelson/d9f703d7e1bc12909b82801ec5050c4b to your computer and use it in GitHub Desktop.
Customize Stream Android single message viewholder
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
class ChannelFragment : Fragment() { | |
//... | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
binding.messageListView.setMessageViewHolderFactory(MyCustomMessageViewHolderFactory()) | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="12dp" | |
> | |
<FrameLayout | |
android:id="@+id/avatar" | |
android:layout_width="40dp" | |
android:layout_height="40dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/avatar_bg" | |
android:background="@android:color/background_dark" | |
/> | |
<TextView | |
android:id="@+id/avatar_initials" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="@android:color/white" | |
android:layout_gravity="center" | |
tools:text="A" | |
/> | |
</FrameLayout> | |
<TextView | |
android:id="@+id/message" | |
android:layout_width="wrap_content" | |
android:layout_height="0dp" | |
android:padding="12dp" | |
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1" | |
app:layout_constraintLeft_toRightOf="@+id/avatar" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:text="Hey everyone! \nI love this Band! What's your favourite song?" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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.chat.sample.feature.channel | |
import android.graphics.Color | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import com.getstream.sdk.chat.adapter.MessageListItem | |
import com.getstream.sdk.chat.adapter.MessageViewHolderFactory | |
import com.getstream.sdk.chat.adapter.viewholder.message.BaseMessageListItemViewHolder | |
import com.getstream.sdk.chat.view.MessageListViewStyle | |
import io.getstream.chat.android.client.models.Channel | |
import io.getstream.chat.android.client.models.initials | |
import io.getstream.chat.sample.databinding.ItemMessageBinding | |
class MyCustomMessageViewHolderFactory : MessageViewHolderFactory() { | |
override fun createMessageViewHolder( | |
parent: ViewGroup, | |
viewType: Int, | |
style: MessageListViewStyle, | |
channel: Channel | |
): BaseMessageListItemViewHolder<*> { | |
return when (viewType) { | |
MESSAGEITEM_MESSAGE -> MyCustomMessageViewHolder(parent) | |
else -> super.createMessageViewHolder(parent, viewType, style, channel) | |
} | |
} | |
} | |
class MyCustomMessageViewHolder( | |
val parent: ViewGroup, | |
val binding: ItemMessageBinding = ItemMessageBinding.inflate(parent.inflater, parent, false) | |
): BaseMessageListItemViewHolder<MessageListItem.MessageItem>(binding.root) { | |
private val colors = listOf(Color.BLUE, Color.CYAN, Color.MAGENTA) | |
override fun bind(item: MessageListItem.MessageItem) { | |
binding.message.text = item.message.text | |
val userId = item.message.user.id | |
val color = userIdWithColor.get(userId) ?: colors.random() | |
if (!userIdWithColor.containsKey(userId)) { | |
userIdWithColor.put(userId, color) | |
} | |
binding.avatarBg.setBackgroundColor(color) | |
binding.avatarInitials.text = item.message.user.initials | |
} | |
companion object { | |
private val userIdWithColor: MutableMap<String, Int> = mutableMapOf() | |
} | |
} | |
internal inline val ViewGroup.inflater: LayoutInflater | |
get() = LayoutInflater.from(context) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment