首页 > 其他 > 详细

How to add Header to recyclerview in kotlin? 怎么样在recyclerview 里添加header Kotlin

时间:2020-04-30 17:03:08      阅读:85      评论:0      收藏:0      [点我收藏+]

How to add Header to recyclerview in kotlin?

class AllCategoryAdapter(val categoryList : List<AllCategoryBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
{
    private val TYPE_HEADER : Int = 0
    private val TYPE_LIST : Int = 1

    override fun getItemViewType(position: Int): Int {

        if(position == 0)
        {
            return TYPE_HEADER
        }
        return TYPE_LIST
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == TYPE_HEADER)
        {
            val header = LayoutInflater.from(parent.context).inflate(R.layout.cv_all_category_header,parent,false)
            return ViewHolderHeader(header)
        }

        val header = LayoutInflater.from(parent.context).inflate(R.layout.cv_all_category,parent,false)
        return ViewHolder(header)
    }

    override fun getItemCount(): Int {
        return categoryList.size + 1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val listItem : AllCategoryBean = categoryList[position]

        if(holder is ViewHolderHeader)
        {
            holder.tvCategoyName.setText("All Category")
        }

        if(holder is ViewHolder)
        {
            holder.tvCategoyName.setText(listItem.getCategoryName())
        }
    }

    class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
    {
        val tvCategoyName = itemView.findViewById(R.id.tvCategoyName) as TextView
    }

    class ViewHolderHeader(itemView : View) : RecyclerView.ViewHolder(itemView)
    {
        val tvCategoyName = itemView.findViewById(R.id.tvCategoyName) as TextView
    }

}

 

How to add Header to recyclerview in kotlin? 怎么样在recyclerview 里添加header Kotlin

原文:https://www.cnblogs.com/xixiaohui/p/12809599.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!