How to highlight filtered text in RecyclerView when using SearchView widget

后端 未结 4 1432
执念已碎
执念已碎 2020-12-30 16:44

How to highlight search text result in RecyclerView. I found some posts regarding Spannable TextView, but not sure where to implement in my case. Appreciate you can look an

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 17:23

            while(index>**-1**){
                       SpannableStringBuilder sb = new SpannableStringBuilder(desc);
                       ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); //specify color here
                       sb.setSpan(fcs, index, **index** + searchText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
                        index = desc.indexOf(searchText,index+1);
    
                    }
    

    A little add-on to the previous answer , you should check index >-1 instead, else the first text wont be highlighted. For setSpan , the int end should be your index + searchText.length().

提交回复
热议问题