1
我在我的應用程序中使用RecyclerView
和自定義適配器... adapter be implements可搜索的可過濾。如何設置高亮度搜索文本?RecyclerView適配器中的高亮度搜索文本
這是我的代碼在自定義適配器過濾:
private Filter filterResult = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Moment> tempList = new ArrayList<>();
if (MOMENT_FILTER != null) {
if (TextUtils.isEmpty(constraint)) {
tempList = (ArrayList<Moment>) MOMENT_FILTER;
} else {
int length = MOMENT_LIST.size();
int i = 0;
while (i < length) {
Moment item = MOMENT_FILTER.get(i);
if (item.getMoment().contains(constraint))
tempList.add(item);
i++;
}
}
}
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
MOMENT_LIST = (ArrayList<Moment>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
}
}
};
@Override
public Filter getFilter() {
return filterResult;
}
這我在活動代碼文本變化(EditText上):
EDT_SEARCH.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
結果與薩赫勒回答OK!
謝謝,但我不會改變力矩(數據庫模型),因爲在任何方法使用! –
添加另一個參數不應該影響您的其他模型。因爲它不需要改變你的其他方法的代碼 –
@ghasemdeh我已經更新了我的答案。它根據您的要求量身定做。請檢查 –