欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android为RecyclerView设置header和footer(RecyclerView.Adapter和BaseQuickAdapter两种方式)

程序员文章站 2022-03-29 23:09:07
RecyclerView.Adapter的方式 private fun initView() { val list = arrayListOf() repeat(9) { list.add(it.toString()) } binding.rvList.adapter = MyAdapter(list) binding.rvList.layoutManager = L....

Android为RecyclerView设置header和footer(RecyclerView.Adapter和BaseQuickAdapter两种方式)

RecyclerView.Adapter的方式

    private fun initView() {
        val list = arrayListOf<String>()
        repeat(9) {
            list.add(it.toString())
        }
        binding.rvList.adapter = MyAdapter(list)
        binding.rvList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }
package maka.components.myheaderfooter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

private const val TYPE_HEADER = 0
private const val TYPE_NORMAL = 1
private const val TYPE_FOOTER = 2

class MyAdapter(private val list: ArrayList<String>) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    var headerView: View? = null
    var footerView: View? = null

    //根据viewType生成不同的View
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return when (viewType) {
            TYPE_HEADER -> {
                headerView =
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_recycler_header, parent, false)
                MyViewHolder(headerView!!)
            }
            TYPE_FOOTER -> {
                footerView =
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_recycler_footer, parent, false)
                MyViewHolder(footerView!!)
            }
            else -> {
                val itemView =
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_recycler_item, parent, false)
                MyViewHolder(itemView)
            }
        }
    }

    //将数据设置到item上
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        //position-1是需要减去header的位置
        val realPosition = position - 1
        holder.tvTest?.text = list[realPosition]
    }

    //获取item总数,包括header和footer,否则会少数据
    override fun getItemCount(): Int {
        return list.size + 2//加上header和footer
    }

    //重写这个方法,很重要,是加入Header和Footer的关键,我们通过判断item的类型,从而绑定不同的view
    override fun getItemViewType(position: Int): Int {
        if (position == 0) return TYPE_HEADER
        if (position == itemCount - 1) return TYPE_FOOTER
        return TYPE_NORMAL
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvTest: TextView? = null

        init {
            when (itemView) {
                footerView -> {
                    footerView?.setOnClickListener {
                        Toast.makeText(itemView.context, "footer", Toast.LENGTH_SHORT).show()
                    }
                }
                headerView -> {
                    headerView?.setOnClickListener {
                        Toast.makeText(itemView.context, "header", Toast.LENGTH_SHORT).show()
                    }
                }
                else -> {
                    tvTest = itemView.findViewById(R.id.tv_test)
                    tvTest?.setOnClickListener {
                        Toast.makeText(
                            itemView.context,
                            "" + (layoutPosition - 1),
                            Toast.LENGTH_SHORT
                        )
                            .show()
                    }
                }
            }
        }
    }

}


BaseQuickAdapter的方式

    //https://github.com/CymChad/BaseRecyclerViewAdapterHelper
    implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4"
    private fun initView() {
        val list = arrayListOf<String>()
        repeat(9) {
            list.add(it.toString())
        }
        val adapter2 = MyAdapter2(R.layout.item_recycler_item)
        adapter2.data = list
        adapter2.addHeaderView(layoutInflater.inflate(R.layout.item_recycler_header, null))
        adapter2.addFooterView(layoutInflater.inflate(R.layout.item_recycler_footer, null))
        binding.rvList.adapter = adapter2
        binding.rvList.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }
package maka.components.myheaderfooter

import android.widget.TextView
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class MyAdapter2(layoutResId: Int) : BaseQuickAdapter<String, BaseViewHolder>(layoutResId) {

    init {
        setOnItemClickListener { _, _, position ->
            Toast.makeText(context, "position=$position", Toast.LENGTH_SHORT).show()
        }
    }

    override fun convert(holder: BaseViewHolder, item: String) {
        val tvTest = holder.getView<TextView>(R.id.tv_test)
        tvTest.text = item
    }
}

本文地址:https://blog.csdn.net/yu540135101/article/details/112513534