使用SpannedString

时间:2018-07-17 07:16:30

标签: android android-recyclerview textview spanned

我正在使用SpannedString为textView中的一些特殊字符着色。当我不使用SpannedString并仅设置纯文本时,一切正常,但是当我使用SpannedString时,滚动速度非常慢且非常缓慢。

我知道onBindViewHolder函数应该非常简单,但是我尝试了不同的方式来存储跨接String并使用了htmlString,但是没有任何改进。 我还使用正则表达式来查找特殊字符,但是在LG设备中,前一个字符也已着色,因此我不得不强制其他字符的颜色为黑色。

我还测试了setHasFixedSize,setExtraLayoutSpace和约束布局;但它并没有改变。

这是我的onBindViewHolder函数:

@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);
}

我该如何解决?

谢谢

here is a screenshot from my recylcerview

2 个答案:

答案 0 :(得分:0)

您需要从onBindViewHolder方法中寻找要突出显示的符号的循环,然后将其转移到模型中。您需要在某个位置计算所有间隔-在以adater输出之前。然后在onBindViewHolder方法中使用它们。

在您的模型中

public class Aye {
    private String aye;
    private ArrayList<SpannableStringBuilder> spans;

    public Aye(String aye, String arabicV, String endSuffix) {
        this.aye = aye;
        this.spans = getSearchSpans(aye, arabicV, endSuffix);
    }

    public String getAye() {
        return aye;
    }

    public ArrayList<SpannableStringBuilder> getSpans() {
        return spans;
    }

    private ArrayList<SpannableStringBuilder> getSearchSpans(String aye, String arabicV, String endSuffix) {
        ArrayList<SpannableStringBuilder> spans = new ArrayList<>();
        for (int j = 0; j < aye.length(); j++) {
            SpannableStringBuilder wordToSpan = new SpannableStringBuilder(aye.substring(j, j + 1));

            if (arabicV.contains(aye.substring(j, j + 1))) {
                wordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor(PrefUtils.getFromPrefs(context, PrefUtils.ARABIC_COLOR, "#FF0000"))), 0, 1, 0);

            } else if (endSuffix.contains(aye.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);
            }

            SpannedString result = (SpannedString) TextUtils.concat(result, "", wordToSpan);
            spans.add(wordToSpan);
        }
        return spans;
    }
}

在您的适配器中

@Override
public void onBindViewHolder(AyeViewHolder holder, int i) {

    String aye = ayeList.get(i).getAye();
    ArrayList<SpannableStringBuilder> ayeSpans = ayeList.get(i). getSpans();

    for (SpannableStringBuilder span : ayeSpans) {
        holder.aye.setText(span, TextView.BufferType.SPANNABLE);
    }
}

答案 1 :(得分:0)

您必须通过尽可能多地使用语句和赋值来放松循环,就像在循环声明以下语句之前那样:

    ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(PrefUtils.getFromPrefs(context, PrefUtils.ARABIC_COLOR, "#FF0000")));
    ForegroundColorSpan colorSpanEnd = new ForegroundColorSpan(Color.parseColor( "#00acc2"));
    ForegroundColorSpan colorSpan0 = new ForegroundColorSpan(Color.parseColor( "#000000"));

并在循环内:

String sub = ayeList.get(i).getAye().substring(j,j+1);

然后:

        if (arabicV.contains(sub)) {
            wordtoSpan.setSpan(colorSpan,0, 1 , 0);

        }else if (endSuffix.contains(sub)) {
            wordtoSpan.setSpan(colorSpanEnd,0, 1 , 0);
        }else {
            wordtoSpan.setSpan(colorSpan0,0, 1, 0);
        }