Created
November 13, 2021 10:06
-
-
Save seoft/55e4c56eec4c4e66b693bcf315ac6d19 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
class NormalAdapter( | |
private val onNormalListener: OnNormalListener | |
) : ListAdapter<NormalUiModel, BindViewHolder<NormalUiModel>>( | |
object : DiffUtil.ItemCallback<NormalUiModel>() { | |
override fun areItemsTheSame(oldItem: NormalUiModel, newItem: NormalUiModel) = | |
oldItem.id == newItem.id | |
override fun areContentsTheSame(oldItem: NormalUiModel, newItem: NormalUiModel) = | |
oldItem == newItem | |
} | |
) { | |
override fun getItemViewType(position: Int): Int { | |
return getItem(position).type.ordinal | |
} | |
override fun onCreateViewHolder( | |
parent: ViewGroup, viewType: Int | |
): BindViewHolder<NormalUiModel> { | |
return when (NormalUiModel.Type.values()[viewType]) { | |
NormalUiModel.Type.MONITOR -> NormalMonitorViewHolder( | |
ItemNormalMonitorBinding.inflate( | |
LayoutInflater.from(parent.context), parent, false | |
), onNormalListener | |
) | |
NormalUiModel.Type.MOUSE -> NormalMouseViewHolder( | |
ItemNormalMouseBinding.inflate( | |
LayoutInflater.from(parent.context), parent, false | |
), onNormalListener | |
) | |
NormalUiModel.Type.PHONE -> NormalPhoneViewHolder( | |
ItemNormalPhoneBinding.inflate( | |
LayoutInflater.from(parent.context), parent, false | |
), onNormalListener | |
) | |
} | |
} | |
override fun onBindViewHolder(holder: BindViewHolder<NormalUiModel>, position: Int) { | |
holder.bind(getItem(position)) | |
} | |
} |
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 NormalMonitorViewHolder( | |
private val binding: ItemNormalMonitorBinding, onNormalListener: OnNormalListener | |
) : BindViewHolder<NormalUiModel>(binding.root) { | |
init { | |
binding.onNormalListener = onNormalListener | |
} | |
override fun bind(item: NormalUiModel) { | |
if (item !is NormalUiModel.Monitor) return | |
binding.item = item | |
} | |
} | |
class NormalMouseViewHolder( | |
private val binding: ItemNormalMouseBinding, onNormalListener: OnNormalListener | |
) : BindViewHolder<NormalUiModel>(binding.root) { | |
init { | |
binding.onNormalListener = onNormalListener | |
} | |
override fun bind(item: NormalUiModel) { | |
if (item !is NormalUiModel.Mouse) return | |
binding.item = item | |
} | |
} | |
class NormalPhoneViewHolder( | |
private val binding: ItemNormalPhoneBinding, onNormalListener: OnNormalListener | |
) : BindViewHolder<NormalUiModel>(binding.root) { | |
init { | |
binding.onNormalListener = onNormalListener | |
} | |
override fun bind(item: NormalUiModel) { | |
if (item !is NormalUiModel.Phone) return | |
binding.item = item | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment