Skip to content

Commit

Permalink
Merge pull request #143 from Ryosuke839/#142-recyclerview-performance
Browse files Browse the repository at this point in the history
#142 Improve RecyclerView performance
  • Loading branch information
Ryosuke839 authored Sep 17, 2023
2 parents 5f3a23d + 5375b35 commit b71f5cf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
34 changes: 32 additions & 2 deletions app/src/main/java/jp/ddo/hotmist/unicodepad/ListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,10 @@ internal class ListAdapter(activity: Activity, pref: SharedPreferences, db: Name
runOnUiThread {
highTarget?.setBackgroundColor(resnormal)
highTarget = null
notifyItemChanged(highlight)
highlight = -1
if (highlight != -1) {
notifyItemChanged(highlight)
highlight = -1
}
}
false
}
Expand Down Expand Up @@ -708,6 +710,34 @@ internal class ListAdapter(activity: Activity, pref: SharedPreferences, db: Name
}
}

override fun instantiateSpanSizeLookup(context: Context, spanCount: Int): GridLayoutManager.SpanSizeLookup {
return object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (getItemViewType(position) != 1 || single) 1 else spanCount
}

override fun getSpanIndex(position: Int, spanCount: Int): Int {
val (itemIndex, titleIndex) = getItemIndex(position)
return if (titleIndex != -1) {
0
} else {
itemIndex % spanCount
}
}

override fun getSpanGroupIndex(adapterPosition: Int, spanCount: Int): Int {
val (itemIndex, titleIndex) = getItemIndex(adapterPosition)
return if (titleIndex != -1) {
titleIndex + (adapterPosition - titleIndex) / spanCount
} else {
adapterPosition - itemIndex + itemIndex / spanCount
}
}
}.also {
it.isSpanIndexCacheEnabled = false
}
}

override fun destroy() {
jump = null
mark = null
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/jp/ddo/hotmist/unicodepad/UnicodeAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ abstract class UnicodeAdapter(protected val activity: Activity, private val db:
return if (itemIndex == -1) -1L - titleIndex else getItemCodePoint(itemIndex)
}

private fun getItemIndex(position: Int): Pair<Int, Int> {
protected fun getItemIndex(position: Int): Pair<Int, Int> {
val titleIndex = searchTitlePosition(position)
return if (titleIndex >= 0 && position == getTitlePosition(titleIndex) + titleIndex)
-1 to titleIndex
Expand Down Expand Up @@ -282,13 +282,19 @@ abstract class UnicodeAdapter(protected val activity: Activity, private val db:

private var layoutManager: GridLayoutManager? = null

open fun instantiateSpanSizeLookup(context: Context, spanCount: Int): GridLayoutManager.SpanSizeLookup {
return object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (getItemViewType(position) != 1 || single) 1 else spanCount
}
}.also {
it.isSpanIndexCacheEnabled = true
}
}

fun getLayoutManager(context: Context, spanCount: Int): GridLayoutManager {
return GridLayoutManager(context, if (single) 1 else spanCount).also {
it.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (getItemViewType(position) != 1 || single) 1 else spanCount
}
}
it.spanSizeLookup = instantiateSpanSizeLookup(context, spanCount)
layoutManager = it
}
}
Expand Down

0 comments on commit b71f5cf

Please sign in to comment.