Channel differentiation allows for a more organized and intuitive channel list within your application. By customizing the channel list to accommodate different channel types, users can have a tailored experience that fits the unique needs of different groups or topics.
First, define a new channel type that starts from 1001 and above. This is because Sendbird UIKit reserves channel types from 0 to 1000.
const val NEW_CHANNEL_TYPE = 1001
When creating new channel types, custom data may be necessary to differentiate messages. Before sending a channel, you can set its custom type and insert the necessary data for rendering the UI.
You can draw new channel items by inheriting from ChannelListAdapter and implementing getItemViewType(). This works in the same way as the Android's RecyclerView. Based on the custom type returned by the getItemViewType() method, the adapter provides the corresponding custom ViewHolder to render the item's layout.
class CustomItemTypeChannelListAdapter : ChannelListAdapter() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<GroupChannel> {
return when (viewType) {
NEW_CHANNEL_TYPE -> CustomItemTypeChannelPreviewHolder(ViewChannelListItemPreviewBinding.inflate(LayoutInflater.from(parent.context), parent, false))
else -> super.onCreateViewHolder(parent, viewType)
}
}
override fun getItemViewType(position: Int): Int = with(getItem(position)) {
return when (this.customType) {
NEW_CHANNEL_TYPE.toString() -> return NEW_CHANNEL_TYPE
else -> super.getItemViewType(position)
}
}
inner class CustomItemTypeChannelPreviewHolder(private val binding: ViewChannelListItemPreviewBinding) : BaseViewHolder<GroupChannel>(binding.getRoot()) {
override fun bind(channel: GroupChannel) {
// Draw your custom channel item view
}
}
}
For an in-depth practical demonstration, see our sample code.