Skip to content

Commit 7e3559c

Browse files
committed
feat (RecyclerSectionItemDecoration): support nullable section info
1 parent 9d6febc commit 7e3559c

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

app/src/main/java/xyz/sangcomz/stickytimeline/MainActivity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ class MainActivity : AppCompatActivity() {
3434
override fun isSection(position: Int): Boolean =
3535
singerList[position].debuted != singerList[position - 1].debuted
3636

37-
override fun getSectionHeader(position: Int): SectionInfo
38-
= SectionInfo(singerList[position].debuted,
39-
singerList[position].group)
37+
override fun getSectionHeader(position: Int): SectionInfo?
38+
= SectionInfo(singerList[position].debuted, singerList[position].group)
4039
}
4140
}
4241

stickytimelineview/src/main/java/xyz/sangcomz/stickytimelineview/RecyclerSectionItemDecoration.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,35 @@ class RecyclerSectionItemDecoration(context: Context,
110110
val topChild = parent.getChildAt(0) ?: return
111111
val topChildPosition = parent.getChildAdapterPosition(topChild)
112112
headerView?.let {
113-
val sectionInfo = sectionCallback.getSectionHeader(topChildPosition)
114-
previousHeader = sectionInfo
115-
setHeaderView(sectionInfo)
116-
val offset =
117-
if (topChildPosition == 0
118-
&& childInContact.top - (headerOffset * 2) == (-1 * headerOffset)) 0f
119-
else
120-
(childInContact.top - (headerOffset * 2)).toFloat()
121-
122-
moveHeader(c, it, offset)
113+
sectionCallback.getSectionHeader(topChildPosition)?.let { sectionInfo ->
114+
previousHeader = sectionInfo
115+
setHeaderView(sectionInfo)
116+
val offset =
117+
if (topChildPosition == 0
118+
&& childInContact.top - (headerOffset * 2) == (-1 * headerOffset)) 0f
119+
else
120+
(childInContact.top - (headerOffset * 2)).toFloat()
121+
122+
moveHeader(c, it, offset)
123+
}
123124
}
124125
}
125126
}
126127

127128
for (i in 0 until parent.childCount) {
128129
val child = parent.getChildAt(i)
129130
val position = parent.getChildAdapterPosition(child)
130-
val sectionInfo = sectionCallback.getSectionHeader(position)
131-
setHeaderView(sectionInfo)
132-
if (previousHeader != sectionInfo) {
133-
headerView?.let {
134-
drawHeader(c, child, it)
135-
previousHeader = sectionInfo
136-
}
131+
sectionCallback.getSectionHeader(position)?.let { sectionInfo ->
132+
setHeaderView(sectionInfo)
133+
if (previousHeader != sectionInfo) {
134+
headerView?.let {
135+
drawHeader(c, child, it)
136+
previousHeader = sectionInfo
137+
}
137138

139+
}
138140
}
141+
139142
}
140143
}
141144

@@ -245,7 +248,7 @@ class RecyclerSectionItemDecoration(context: Context,
245248

246249
fun isSection(position: Int): Boolean
247250

248-
fun getSectionHeader(position: Int): SectionInfo
251+
fun getSectionHeader(position: Int): SectionInfo?
249252
}
250253
}
251254

0 commit comments

Comments
 (0)