By default, Sendbird UIKit for Android supports multi-selection in the user list. This section guides you on how to customize the number of selections available.
To customize the selection, you need to override the interaction within the ViewHolder where the selectedUserIdList is modified through adding or removing. This requires creating a custom ViewHolder and Adapter classes. For additional information, refer to the applying custom adapters page.
The following example is an implementation of a CreateChannelUserListAdapter that limits the number of selectable users.
class CustomSelectionCreateChannelUserListAdapter(private val maxSelectionCount: Int) : CreateChannelUserListAdapter() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<UserInfo> {
return SelectUserViewHolder(ViewSelectUserBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: BaseViewHolder<UserInfo>, position: Int) {
val userInfo = getItem(position)
holder.bind(userInfo)
}
inner class SelectUserViewHolder(internal val binding: ViewSelectUserBinding) : BaseViewHolder<UserInfo>(binding.root) {
init {
binding.root.setOnClickListener { v ->
val userPosition = bindingAdapterPosition
if (userPosition != RecyclerView.NO_POSITION) {
if (onCheckChanged(userPosition)) {
binding.cbUserPreview.toggle()
}
}
}
binding.cbUserPreview.setOnClickListener { v ->
val userPosition = bindingAdapterPosition
if (userPosition != RecyclerView.NO_POSITION) {
if (!onCheckChanged(userPosition)) {
binding.cbUserPreview.toggle()
}
}
}
}
private fun onCheckChanged(userPosition: Int): Boolean {
val userInfo: UserInfo = getItem(userPosition)
val isSelected: Boolean = isSelected(userInfo)
if (!isSelected) {
val totalSelectionCount = selectedUserIdList.count()
if (totalSelectionCount == maxSelectionCount) {
if (maxSelectionCount == 1) {
// for single selection, replace previous selection
selectedUserIdList.removeFirst()?.let { userId ->
val removedIndex = userList.indexOfFirst { it.userId == userId }
if (removedIndex != RecyclerView.NO_POSITION) {
notifyItemChanged(removedIndex)
}
}
} else {
// for multiple selection, prevent additional selection
return false
}
}
selectedUserIdList.add(userInfo.userId)
} else {
selectedUserIdList.remove(userInfo.userId)
}
// listener to allow CreateChannelFragment to receive events when user selection is changed
// so that the header's right button can be enabled/disabled accordingly
onUserSelectChangedListener?.onUserSelectChanged(selectedUserIdList, isSelected)
return true
}
override fun bind(item: UserInfo) {
// bind view holder
}
}
}