1
0
Fork 0

for #1104 - Fixes logic in Sectioned Adapter

master
Jeff Boek 2019-03-22 15:16:34 -07:00 committed by Colin Lee
parent 9c969f63ea
commit b3ac37078e
1 changed files with 17 additions and 11 deletions

View File

@ -46,9 +46,13 @@ abstract class SectionedAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>(
return sectionTypeForPosition(position).viewType return sectionTypeForPosition(position).viewType
} }
override fun getItemCount(): Int { final override fun getItemCount(): Int {
val numberOfSections = numberOfSections() var count = 0
return numberOfSections + (0..numberOfSections).reduce { a, b -> a + numberOfRowsInSection(b) } for (i in 0 until numberOfSections()) {
count += numberOfRowsInSection(i) + 1
}
return count
} }
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
@ -61,16 +65,18 @@ abstract class SectionedAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>(
} }
private fun sectionTypeForPosition(position: Int): SectionType { private fun sectionTypeForPosition(position: Int): SectionType {
var counter = 0 var currentPosition = 0
for (section in 0..numberOfSections()) { for (sectionIndex in 0 until numberOfSections()) {
if (counter == position) { return SectionType.Header(section) } if (position == currentPosition) { return SectionType.Header(sectionIndex) }
counter += 1 currentPosition +=1
for (row in 0..numberOfRowsInSection(section)) { for (rowIndex in 0 until numberOfRowsInSection(sectionIndex)) {
if (counter == position) { return SectionType.Row(section, row) } if (currentPosition == position) { return SectionType.Row(sectionIndex, rowIndex) }
counter += 1 currentPosition += 1
} }
} }
throw IllegalStateException("hello world!")
} }
} }