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
        }
    }

}

比较清晰的例子

上一篇:Anaconda安装Pyecharts


下一篇:Python轻松实现地图可视化(附详细源码)