比如对象有个type属性(Int类型)
//type : 0,Start (起点) ; 1, End (终点) ; 2,Walk (步行) ; 3,Bus (公交)
data class PlanResultDetailsView(val type : Int , val result : String) {
}
Adapter代码:
class PlanResultDetailsViewAdapter(private val itemList: List<PlanResultDetailsView>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
private const val START_VIEW = 0
private const val END_VIEW = 1
private const val WALK_VIEW = 2
private const val BUS_VIEW = 3
}
override fun getItemViewType(position: Int): Int {
return itemList[position].type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
START_VIEW -> {
StartViewHolder( AdapterPlanResultStartItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
END_VIEW -> {
EndViewHolder(AdapterPlanResultEndItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
WALK_VIEW -> {
WalkViewHolder(AdapterPlanResultWalkItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
BUS_VIEW -> {
BusViewHolder(AdapterPlanResultBusItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
else -> throw IllegalArgumentException("Invalid view type")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is StartViewHolder -> holder.bind(itemList[position])
is EndViewHolder -> holder.bind(itemList[position])
is WalkViewHolder -> holder.bind(itemList[position])
is BusViewHolder -> holder.bind(itemList[position])
}
}
override fun getItemCount(): Int = itemList.size
class StartViewHolder(private val binding: AdapterPlanResultStartItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: PlanResultDetailsView) {
}
}
class EndViewHolder(private val binding: AdapterPlanResultEndItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: PlanResultDetailsView) {
}
}
class WalkViewHolder(private val binding: AdapterPlanResultWalkItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: PlanResultDetailsView) {
}
}
class BusViewHolder(private val binding: AdapterPlanResultBusItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: PlanResultDetailsView) {
}
}
}

1139

被折叠的 条评论
为什么被折叠?



