其他分享
首页 > 其他分享> > Kotlin Recyclerview多条目形态

Kotlin Recyclerview多条目形态

作者:互联网

class MainAdapter(var context:Context,var list:List<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    class OneViewHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var username:TextView=itemview.orderone_pushtime
    }

    class TwoViewHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var username:TextView=itemview.ordertwo_stage
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType==100){
            val inflate = LayoutInflater.from(context).inflate(R.layout.cleanorderone_adapter_layout, parent, false)
            return OneViewHolder(inflate)
        }else{
            val inflate = LayoutInflater.from(context).inflate(R.layout.cleanordertwo_adapter_layout,parent,false)
            return TwoViewHolder(inflate)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemViewType = getItemViewType(position)
        if (itemViewType==100){
            (holder as OneViewHolder).username.text="我是第一种布局"
        }else{
            (holder as TwoViewHolder).username.text="我是第二种布局"
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        if (list.get(position).equals("李四")){
            return 100
        }else{
            return 200
        }
    }

}

比较清晰的例子

标签:ViewHolder,return,itemview,Int,Kotlin,条目,RecyclerView,inflate,Recyclerview
来源: https://blog.csdn.net/ZhangXuxiaoqingnian/article/details/119422912