在nougat 7.0 / 7.1上,SpannableStringBuilder.getSpans()排序顺序错误

时间:2016-12-09 02:27:27

标签: android android-7.0-nougat spannablestringbuilder

以下代码适用于我在2.3和6.0.1之间尝试的所有内容,但无法使用7.0 / Emulator和7.1.1 / Nexus 6P

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
for (int i=0;i<5;++i) {
    int p = spannableStringBuilder.length();
    spannableStringBuilder.append("aAAAAa\n");
    spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), p+1, p+5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

boolean pass = true;
StyleSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), StyleSpan.class);
int p = 0;
for (StyleSpan span : spans) {
    int start = spannableStringBuilder.getSpanStart(span);
    if (start<p) {
        pass = false;
        break;
    }
    p = start;
}

1 个答案:

答案 0 :(得分:3)

当前的解决方法我正在使用

Arrays.sort(spans, new Comparator<ClickableSpan>() {
    @Override
    public int compare(ClickableSpan o1, ClickableSpan o2) {
        return s.getSpanStart(o1)-s.getSpanStart(o2);
    }
});