0

This question may have been asked before, but I could not find the solution to the exact problem I am facing. I need to add a footer to my RecyclerView such that the footer scrolls with the rest of the items. I took inspiration from this article to create a FooterDecoration that adds a layout to the end of the list. https://stackoverflow.com/a/33458367

I also have DividerItemDecoration added in addition to the FooterDecoration.

The problem with this approach is that the footer is getting added before the last divider item instead of after it. How do I add the footer such that it gets added after the last item divider?

This code is in Kotlin.

class FooterDecoration(
    val context: Context,
    val parent: RecyclerView,
    var layout: View
) : RecyclerView.ItemDecoration() {
  init {
    layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.AT_MOST),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
  }

  override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
    super.onDraw(c, parent, state)     
    layout.layout(parent.left, 0, parent.right, layout.measuredHeight)

    for (i in 0..parent.childCount - 1) {
      val view = parent.getChildAt(i)
      if (parent.getChildAdapterPosition(view) == parent.adapter.itemCount.minus(1)) {
        c.save()
        val top = view.bottom
        c.translate(0f, top.toFloat())
        layout.draw(c)
        c.restore()
        break
      }
    }
  }

  override fun getItemOffsets(
      outRect: Rect,
      view: View,
      parent: RecyclerView,
      state: RecyclerView.State
  ) {
    if (parent.getChildAdapterPosition(view) == parent.adapter.itemCount.minus(1)) {
      outRect.set(0, 0, 0, layout.measuredHeight)
    } else {
      outRect.setEmpty()
    }
  }
}

It might be helpful to know that I am adding DividerItemDecoration before FooterDecoration.

What am I missing? Thanks for any help.

Adapter code:

class PlanCategoryAdapter(
    val planNodes: List<Nodes>,
    val context: Context
) : RecyclerView.Adapter<CategoryViewHolder>() {

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

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
    val view = LayoutInflater.from(context).inflate(R.layout.view_category_item, parent, false)
    return CategoryViewHolder(view)
  }

  override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
    val searchNode = planNodes[position]
    holder.bind(searchNode)
  }
}

class CategoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

  val tvTitle: TextView by bindView(R.id.tvTitle)

  fun bind(planNode: Node) {
    tvTitle.text = planNode.displayName
  }
}
Community
  • 1
  • 1
Nidhi Shah
  • 568
  • 2
  • 7
  • 14
  • 1
    In case of less items in list, like 1 or 2, then where should this footer placed? Bottom of screen or just below of last item? – Pankaj Kumar Mar 03 '17 at 05:26
  • Why would you add a footer to RecyclerView if you want it to be sticky. If you implement your footer below the RecyclerView as a separate view, it will always remain sticky. – Swas_99 Mar 03 '17 at 05:42
  • @PankajKumar I want it to be placed below the last item. – Nidhi Shah Mar 03 '17 at 05:45
  • @Swas_99 I want the footer to scroll with the rest of the items. – Nidhi Shah Mar 03 '17 at 05:48
  • https://gist.github.com/ajaydewari/fe1bee4b86867ce43d160d43a5abe49c, In place of view type make it fotter and on function, getViewType(int position){if(item.size==position) return fottertype} – HAXM Mar 03 '17 at 06:21
  • post your adapter I will modify it for you. – HAXM Mar 03 '17 at 06:23
  • Thanks @HAXM for responding. I am aware of this solution, but I am trying to see if I can add the footer without modifying adapter code. If this doesn't work, I will fall back to updating adapter. At this point though, I am curious to learn the problem with this approach. – Nidhi Shah Mar 03 '17 at 06:25
  • You can use recyclerview inside scrollView with the fotter. and make the fotter vissable only if latoutmanager.getItem...>0, hop this simple solution will help. – HAXM Mar 03 '17 at 06:29
  • @HAXM Updated the question with adapter code. – Nidhi Shah Mar 03 '17 at 06:31

0 Answers0