RecyclerView 根据类型加载不同ItemView

比如对象有个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) {
            
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唉盆u

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值