How implement sticky footer in recyclerview

后端 未结 8 1876
时光说笑
时光说笑 2021-02-04 02:52

I have RecyclerView and I need next behavior:

  • if there are a lot of items (more then fits screen) - footer is last item
  • if few item/no item - footer is lo
8条回答
  •  轮回少年
    2021-02-04 03:29

    class FooterViewHolder(private val parent: ViewGroup) {
    
    ...
    
    fun bind(item: Item) {
        ...
        itemView.post(::adjustTop)
    }
    
    private fun adjustTop() {
        val parent = parent as RecyclerView
        var topOffset = parent.height
        for (child in parent.children) topOffset -= child.height
        (itemView.layoutParams as ViewGroup.MarginLayoutParams)
            .setMargins(0, topOffset.coerceAtLeast(0), 0, 0)
    }
    }
    

提交回复
热议问题