-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support setting color of activated indicator
- Loading branch information
Showing
5 changed files
with
110 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
indicator-fast-scroll/src/main/java/com/reddit/indicatorfastscroll/TextColorUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.reddit.indicatorfastscroll | ||
|
||
import android.text.SpannableString | ||
import android.text.Spanned | ||
import android.text.style.ForegroundColorSpan | ||
import android.widget.TextView | ||
import androidx.core.text.clearSpans | ||
|
||
internal object TextColorUtil { | ||
|
||
fun highlightAtIndex(textView: TextView, highlightedIndex: Int?, color: Int) { | ||
textView.apply { | ||
if (highlightedIndex == null) { | ||
clearHighlight(this) | ||
} else { | ||
text = SpannableString.valueOf(text).apply { | ||
clearSpans() | ||
val linesUpToHighlight = lineSequence().take(highlightedIndex + 1).toList() // inclusive | ||
val start = linesUpToHighlight | ||
.dropLast(1) | ||
.fold(0) { acc, line -> | ||
acc + line.length + 1 | ||
} | ||
val highlightedLineSize = linesUpToHighlight.lastOrNull()?.length ?: 0 | ||
|
||
setSpan(ForegroundColorSpan(color), start, start + highlightedLineSize, 0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun clearHighlight(textView: TextView) { | ||
textView.apply { | ||
if (text is Spanned) { | ||
text = SpannableString.valueOf(text).apply { | ||
clearSpans() | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:color="@color/colorAccent" android:state_activated="true" /> | ||
<item android:color="@color/colorPrimary" android:state_pressed="true" /> | ||
<item android:color="@android:color/black" /> | ||
</selector> |