@@ -10,36 +10,45 @@ class PinnedHeaderItemDecoration(private val listener: PinnedHeaderListener) :
1010 RecyclerView .ItemDecoration () {
1111
1212 private var headerHeight: Int = 0
13+ private var cachedHeader: View ? = null
14+ private var cachedHeaderPosition: Int = RecyclerView .NO_POSITION
1315
1416 override fun onDrawOver (c : Canvas , parent : RecyclerView , state : RecyclerView .State ) {
1517 super .onDrawOver(c, parent, state)
1618
1719 val topChild = parent.getChildAt(0 ) ? : return
18-
1920 val topChildPosition = parent.getChildAdapterPosition(topChild)
20- if (topChildPosition == RecyclerView .NO_POSITION ) {
21- return
21+ if (topChildPosition == RecyclerView .NO_POSITION ) return
22+
23+ val headerPosition = listener.getHeaderPositionForItem(topChildPosition)
24+ val currentHeader = if (cachedHeaderPosition == headerPosition && cachedHeader != null ) {
25+ cachedHeader!!
26+ } else {
27+ getHeaderViewForItem(headerPosition, parent)
2228 }
2329
24- val currentHeader = getHeaderViewForItem(topChildPosition, parent)
2530 fixLayoutSize(parent, currentHeader)
2631 val contactPoint = currentHeader.bottom
27- val childInContact = getChildInContact(parent, contactPoint) ? : return
32+ val childInContact = getChildInContact(parent, contactPoint)
2833
29- if (listener.isHeader(parent.getChildAdapterPosition(childInContact))) {
34+ if (childInContact != null && listener.isHeader(parent.getChildAdapterPosition(childInContact))) {
3035 moveHeader(c, currentHeader, childInContact)
31- return
36+ } else {
37+ drawHeader(c, currentHeader)
3238 }
33-
34- drawHeader(c, currentHeader)
3539 }
3640
3741 private fun getHeaderViewForItem (itemPosition : Int , parent : RecyclerView ): View {
3842 val headerPosition = listener.getHeaderPositionForItem(itemPosition)
39- val layoutResId = listener.getHeaderLayout(headerPosition)
40- val header = LayoutInflater .from(parent.context).inflate(layoutResId, parent, false )
41- listener.bindHeaderData(header, headerPosition)
42- return header
43+
44+ if (cachedHeaderPosition != headerPosition || cachedHeader == null ) {
45+ val layoutResId = listener.getHeaderLayout(headerPosition)
46+ val header = LayoutInflater .from(parent.context).inflate(layoutResId, parent, false )
47+ listener.bindHeaderData(header, headerPosition)
48+ cachedHeader = header
49+ cachedHeaderPosition = headerPosition
50+ }
51+ return cachedHeader!!
4352 }
4453
4554 private fun drawHeader (c : Canvas , header : View ) {
@@ -57,18 +66,9 @@ class PinnedHeaderItemDecoration(private val listener: PinnedHeaderListener) :
5766 }
5867
5968 private fun getChildInContact (parent : RecyclerView , contactPoint : Int ): View ? {
60- var childInContact: View ? = null
61- for (i in 0 until parent.childCount) {
62- val child = parent.getChildAt(i)
63- if (child.bottom > contactPoint) {
64- if (child.top <= contactPoint) {
65- // This child overlaps the contactPoint
66- childInContact = child
67- break
68- }
69- }
70- }
71- return childInContact
69+ return (0 until parent.childCount)
70+ .map { parent.getChildAt(it) }
71+ .firstOrNull { it.bottom > contactPoint && it.top <= contactPoint }
7272 }
7373
7474 /* *
0 commit comments