I'm implementing a sticky header item decoration, but I'm trying to make the header overlay the item. I'm basing the code on timehop's library.
https://github.com/timehop/sticky-headers-recyclerview
With how it's designed, the item decoration will still create a row, but I want it have a height of 0 within the actual list.
Here is an example of what I'm trying to accomplish.
And here is the code for the current sticky item decoration that takes creates its own row. There are a few areas that I've played around with by changing the Rect
it uses, but I cannot get the right result.
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val itemPosition = parent.getChildAdapterPosition(view)
if (itemPosition == RecyclerView.NO_POSITION) {
return
}
if (mHeaderPositionCalculator.hasNewHeader(itemPosition, mOrientationProvider.isReverseLayout(parent))) {
val header = getHeaderView(parent, itemPosition)
setItemOffsetsForHeader(outRect, header, mOrientationProvider.getOrientation(parent))
}
}
/**
* Sets the offsets for the first item in a section to make room for the header view
*
* @param itemOffsets rectangle to define offsets for the item
* @param header view used to calculate offset for the item
* @param orientation used to calculate offset for the item
*/
private fun setItemOffsetsForHeader(itemOffsets: Rect, header: View, orientation: Int) {
mDimensionCalculator.initMargins(mTempRect, header)
// should I modify itemOffsets here?
if (orientation == LinearLayoutManager.VERTICAL) {
itemOffsets.top =header.height + mTempRect.top + mTempRect.bottom
} else {
itemOffsets.left = header.width + mTempRect.left + mTempRect.right
}
}
override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDrawOver(canvas, parent, state)
val childCount = parent.childCount
if (childCount <= 0 || mAdapter.itemCount <= 0) {
return
}
for (i in 0 until childCount) {
val itemView = parent.getChildAt(i)
val position = parent.getChildAdapterPosition(itemView)
if (position == RecyclerView.NO_POSITION) {
continue
}
val hasStickyHeader = mHeaderPositionCalculator.hasStickyHeader(itemView, mOrientationProvider.getOrientation(parent), position)
if (hasStickyHeader || mHeaderPositionCalculator.hasNewHeader(position, mOrientationProvider.isReverseLayout(parent))) {
val header = mHeaderProvider.getHeader(parent, position)
//re-use existing Rect, if any.
var headerOffset: Rect? = mHeaderRects.get(position)
if (headerOffset == null) {
headerOffset = Rect()
mHeaderRects.put(position, headerOffset)
}
mHeaderPositionCalculator.initHeaderBounds(headerOffset, parent, header, itemView, hasStickyHeader)
// should I modify headerOffset here?
mRenderer.drawHeader(parent, canvas, header, headerOffset)
}
}
}