diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-12-20 15:43:38 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-12-21 13:49:02 +0300 |
commit | 88b8f9583ba6f1c6f1d3ca1f4125d6593fe388bb (patch) | |
tree | c5ca1d260edbeacd40be34b8bc5b00b3932f77f1 | |
parent | bcc8723e6cc349b3a2ac3c32731898635d02fe40 (diff) |
Enhance rendering performance of TextView when highlighting search results
Signed-off-by: Stefan Niedermann <info@niedermann.it>
2 files changed, 18 insertions, 8 deletions
diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java index d9ac64936..465aec405 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java @@ -256,7 +256,7 @@ public class MarkwonMarkdownUtil { return false; } - public static void searchAndColor(@NonNull Editable editable, @Nullable CharSequence searchText, @NonNull Context context, @Nullable Integer current, @ColorInt int mainColor) { + public static void searchAndColor(@NonNull Spannable editable, @Nullable CharSequence searchText, @NonNull Context context, @Nullable Integer current, @ColorInt int mainColor) { removeSpans(editable, SearchSpan.class); if (searchText != null) { final Matcher m = Pattern @@ -273,9 +273,9 @@ public class MarkwonMarkdownUtil { } } - private static <T> void removeSpans(@NonNull Editable editable, @SuppressWarnings("SameParameterValue") Class<T> spanType) { - for (T span : editable.getSpans(0, editable.length(), spanType)) { - editable.removeSpan(span); + private static <T> void removeSpans(@NonNull Spannable spannable, @SuppressWarnings("SameParameterValue") Class<T> spanType) { + for (T span : spannable.getSpans(0, spannable.length(), spanType)) { + spannable.removeSpan(span); } } } diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java index a12bc819e..ff144054b 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/plugins/SearchHighlightPlugin.java @@ -1,8 +1,10 @@ package it.niedermann.android.markdown.markwon.plugins; import android.content.Context; -import android.text.Editable; +import android.text.Spannable; +import android.text.SpannableString; import android.text.SpannableStringBuilder; +import android.util.Log; import android.widget.TextView; import androidx.annotation.ColorInt; @@ -17,6 +19,8 @@ import it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil; public class SearchHighlightPlugin extends AbstractMarkwonPlugin { + private static final String TAG = SearchHighlightPlugin.class.getSimpleName(); + private boolean searchActive = false; private CharSequence searchText; private Integer current; @@ -47,9 +51,15 @@ public class SearchHighlightPlugin extends AbstractMarkwonPlugin { public void afterSetText(@NonNull TextView textView) { super.afterSetText(textView); if (this.searchActive) { - final Editable coloredContent = new SpannableStringBuilder(textView.getText()); - MarkwonMarkdownUtil.searchAndColor(coloredContent, searchText, textView.getContext(), current, color); - textView.setText(coloredContent, TextView.BufferType.SPANNABLE); + final CharSequence content = textView.getText(); + if (content.getClass() == SpannableString.class || content instanceof Spannable) { + MarkwonMarkdownUtil.searchAndColor((Spannable) content, searchText, textView.getContext(), current, color); + } else { + Log.w(TAG, "Expected " + TextView.class.getSimpleName() + " content to be of type " + Spannable.class.getSimpleName() + ", but was of type " + content.getClass() + ". Search highlighting will be not performant."); + final Spannable coloredContent = new SpannableStringBuilder(content); + textView.setText(coloredContent, TextView.BufferType.SPANNABLE); + MarkwonMarkdownUtil.searchAndColor(coloredContent, searchText, textView.getContext(), current, color); + } } } } |