How to highlight the filtered text while using SearchView widget in android

后端 未结 3 1041
误落风尘
误落风尘 2021-02-15 07:30

I have implemented SearchView Widget in my app. Its working fine. Now i need to do is, whenever i type a word in my SearchView Bar

3条回答
  •  感情败类
    2021-02-15 08:08

    You Should do this in onBindViewHolder() method (using RecyclerView)

    class YourAdapter

    String searchString="";
    
    @Override
        public void onBindViewHolder(AllMessageAdapter.DataObjectHolder holder, final int position) {
    
            holder.message.setText(mDataset.get(position).Message);
    
            AllMessageList.Message message=mDataset.get(position);
    
            String name = message.Message.toLowerCase(Locale.getDefault());
    
            if (name.contains(searchString)) {
    
                int startPos = name.indexOf(searchString);
                int endPos = startPos + searchString.length();
    
                Spannable spanString = Spannable.Factory.getInstance().newSpannable(holder.message.getText());
                spanString.setSpan(new ForegroundColorSpan(Color.RED), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                holder.message.setText(spanString);
            }
    
        }
    

    Your Filter(in Adapter)

    public void setFilter(ArrayList countryModels,String searchString) {
            this.searchString=searchString;
            mDataset = new ArrayList<>();
            mDataset.addAll(countryModels);
            notifyDataSetChanged();
        }
    

提交回复
热议问题