首先的效果圖
搜索到結(jié)果(這里我只是模擬數(shù)據(jù),真正和服務(wù)器走得時候,返回來的數(shù)據(jù)都應(yīng)該包含關(guān)鍵字的)

模擬的沒有搜索結(jié)果的界面

具體實現(xiàn)
在這插一句哈,就是做一件事情,拆分成多個小結(jié),不至于在開發(fā)的時候摸不著頭腦而且還能把控開發(fā)的進(jìn)度.
思路其實很簡單,我們監(jiān)聽輸入框的變化,然后在文字變化之后去請求服務(wù)器,然后取到我們需要的結(jié)果,進(jìn)行數(shù)據(jù)展示即可.
- 第一步:搜索框的監(jiān)聽
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/**
* 當(dāng)搜索框中的文字發(fā)生變化的時候回調(diào)此方法
* @param charSequence 輸入框的文字
* @param start 開始
* @param before
* @param count 字?jǐn)?shù)
*/
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//在這里進(jìn)行邏輯請求
}
@Override
public void afterTextChanged(Editable s) {
}
});
- 第二步:進(jìn)行相關(guān)邏輯請求
if (!TextUtils.isEmpty(charSequence) && charSequence.length() > 3) { //這里的3只是為了模擬請求
mKey = charSequence.toString();
initData(charSequence.toString());
changeStates(STATE);
} else {
STATE = NO_TTHING;
changeStates(STATE);
}
/**
* 首次獲取數(shù)據(jù)
*
* @param key 高亮值
*/
private void initData(String key) {
//這里是模擬網(wǎng)絡(luò)請求的 實際就是走網(wǎng)絡(luò)獲取數(shù)據(jù)
String result = JsonUtils.getJson(this, "search.json");
Gson gson = new Gson();
SearchBean searchBean = gson.fromJson(result, SearchBean.class);
if (searchBean != null) {
mDataBeen = searchBean.getData();
if (mDataBeen != null && mDataBeen.size() > 0) {
STATE = SHOW_DATA;
mSearchAdapter.loadData(mDataBeen, key);
} else {
STATE = NO_TTHING;
}
} else {
STATE = NO_TTHING;
}
}
/**
* 改變搜索狀態(tài)
*
* @param state 搜索key值
*/
private void changeStates(int state) {
switch (state) {
case NO_TTHING:
mNoLayout.setVisibility(View.VISIBLE);
recycler_view.setVisibility(View.INVISIBLE);
break;
case SHOW_DATA:
mNoLayout.setVisibility(View.GONE);
recycler_view.setVisibility(View.VISIBLE);
break;
}
}
- 第三步:進(jìn)行變色
/**
* @param context 上下文
* @param wholeStr 全部文字
* @param highlightStr 改變顏色的文字
* @param color 顏色
*/
public StringFormatUtil(Context context, String wholeStr, String highlightStr, int color) {
this.mContext = context;
this.wholeStr = wholeStr;
this.highlightStr = highlightStr;
this.color = color;
}
/**
* 填充顏色
*
* @return StringFormatUtil
*/
public StringFormatUtil fillColor() {
if (!TextUtils.isEmpty(wholeStr) && !TextUtils.isEmpty(highlightStr)) {
spBuilder = new SpannableStringBuilder(wholeStr);
//匹配規(guī)則
Pattern p = Pattern.compile(highlightStr);
//匹配字段
Matcher m = p.matcher(spBuilder);
//上色
color = mContext.getResources().getColor(color);
//開始循環(huán)查找里面是否包含關(guān)鍵字 使得一句話中出現(xiàn)多個關(guān)鍵詞都會被高亮
while (m.find()) {
int start = m.start();
int end = m.end();
spBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return this;
}
return null;
}
/**
* 獲取到已經(jīng)更改好的結(jié)果(這個時候已經(jīng)實現(xiàn)了高亮,在獲取這個result的時候不要toString()要不然會把色調(diào)去除的)
*
* @return result
*/
public SpannableStringBuilder getResult() {
if (spBuilder != null) {
return spBuilder;
}
return null;
}
// 進(jìn)行工具類使用,也就是在給title賦值的時候使用
//這個是adapter里面的使用規(guī)則
mFormatUtil = new StringFormatUtil(holder.itemView.getContext(), dataBean.getTitle(), mLightStr, R.color.colorAccent).fillColor();
holder.tv_title.setText(mFormatUtil.getResult());
Demo說明
這里的本地的json是我自己人為定義的,而且在搜索的時候加入了自己的邏輯,如果是實際工程中需要自己根據(jù)自己的需求來進(jìn)行變更的.相關(guān)顯示不需要在意,這里只是給大家一個實現(xiàn)搜索的并且關(guān)鍵詞高亮的一個思路。