Skip to content

Instantly share code, notes, and snippets.

@DVegasa
Created July 13, 2019 11:13
Show Gist options
  • Save DVegasa/9521816bbc817c46b51e9b304d398630 to your computer and use it in GitHub Desktop.
Save DVegasa/9521816bbc817c46b51e9b304d398630 to your computer and use it in GitHub Desktop.
private fun highlight() {
val s = etSearchBar.text
adapter.list.forEach { item ->
// {1}
item.abbr.getSpans(0, item.abbr.length, ForegroundColorSpan::class.java).forEach {
item.abbr.removeSpan(it)
}
// {2}
item.name.getSpans(0, item.name.length, ForegroundColorSpan::class.java).forEach {
item.name.removeSpan(it)
}
// {3}
if (item.abbr.startsWith(s, true)) {
item.abbr.setSpan(spanHighlight, 0, s.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
// {4}
if (item.name.contains(s, true)) {
val index = item.name.toString().indexOf(s.toString(), 0, true)
item.name.setSpan(spanHighlight, index, index + s.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment