I'm using SpannedString for coloring some specials character in textView. When I'm not using SpannedString and just set plain text, everything works fine, But when I use SpannedString, scrolling is very slow and is very laggy.
I know that onBindViewHolder function should be very simple but I tried different ways to store spanned String and also used htmlString, but got no improvements. I also use regex to find special characters, but in LG devices, the previous character is colored too and I have to force other characters' color to be black.
I also test setHasFixedSize, setExtraLayoutSpace, and constraint-layout; but it doesn't change.
Here is my onBindViewHolder function:
@Override
public void onBindViewHolder(AyeViewHolder holder, int i) {
holder.setIsRecyclable(false);
SpannedString result = new SpannedString("");
for (int j = 0; j < ayeList.get(i).getAye().length(); j++) {
SpannableStringBuilder wordtoSpan = new SpannableStringBuilder(ayeList.get(i).getAye().substring(j,j+1));
if (arabicV.contains(ayeList.get(i).getAye().substring(j, j + 1))) {
wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(PrefUtils.getFromPrefs(context,
PrefUtils.ARABIC_COLOR, "#FF0000"))),
0, 1 , 0);
}else if (endSuffix.contains(ayeList.get(i).getAye().substring(j, j + 1))) {
wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor( "#00acc2")),
0, 1 , 0);
}else {
wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#000000")),
0, 1, 0);
}
result = (SpannedString) TextUtils.concat(result,"",wordtoSpan);
}
holder.aye.setText(result, TextView.BufferType.SPANNABLE);
}
How can I fix this?
Thanks