RecyclerView粘滞页脚与DividerItemDecoration

这个问题以前可能已经被问过了,但我找不到解决我所面临的确切问题的方法。 我需要添加页脚到我的RecyclerView,使页脚与其余的项目滚动。 我从这篇文章中获得了灵感来创建一个FooterDecoration,它将一个布局添加到列表的最后。 https://stackoverflow.com/a/33458367

除了FooterDecoration,我还添加了DividerItemDecoration。

这种方法的问题是,页脚被添加到最后的divider项目之前,而不是之后。 如何添加页脚使其在最后一个项目分隔符之后被添加?

这个代码在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() } } } 

知道我在FooterDecoration之前添加DividerItemDecoration可能会有帮助。

我错过了什么? 感谢您的帮助。

适配器代码:

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

Interesting Posts