SearchListAdapter.java
RecyclerView List使用EditText实现可过滤的列表搜索不起作用。请查看此代码。在我的情况下,列表已被过滤,但由于哪个列表保持不变,因此不会更改recyclerView适配器。
/**
* Created by Dell on 4/8/2016.
*/
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> implements Filterable {
private Context context;
public static ArrayList<ChapterListModel> chapterList;
private List<ChapterListModel> orignalChapterList = new ArrayList<>();
private final List<ChapterListModel> filteredChapterList;
public SearchListAdapter(Context context, ArrayList<ChapterListModel> chapterList) {
this.context = context;
this.chapterList = chapterList;
orignalChapterList = chapterList;
filteredChapterList = new ArrayList<>();
}
@Override
public SearchListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_chapter_list, parent, false)
);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.title.setText(chapterList.get(position).getTitle());
}
@Override
public int getItemCount() {
return chapterList.size();
}
@Override
public Filter getFilter() {
return new CustomFilter(this, orignalChapterList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.textView);
}
}
public class CustomFilter extends Filter {
private SearchListAdapter adapter;
private final List<ChapterListModel> originalList;
private final List<ChapterListModel> filteredList;
private CustomFilter(SearchListAdapter adapter, List<ChapterListModel> originalList) {
super();
this.adapter = adapter;
this.originalList = new LinkedList<>(originalList);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final ChapterListModel chapter : originalList) {
if (chapter.getTitle().contains(filterPattern)) {
filteredList.add(chapter);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filteredChapterList.clear();
adapter.filteredChapterList.addAll((List<ChapterListModel>) results.values);
adapter.notifyDataSetChanged();
}
}
}
答案 0 :(得分:2)
最后完成但是使用另一种方式,而不是实现fiterable。
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
final String query = s.toString().toLowerCase().trim();
final ArrayList<ChapterListModel> filteredList = new ArrayList<>();
for (int i = 0; i < chapterLists.size(); i++) {
final String text = chapterLists.get(i).getTitle().toLowerCase();
if (text.contains(query)) {
filteredList.add(chapterLists.get(i));
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
adapter = new SearchListAdapter(SearchActivity.this, filteredList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged(); // data set changed
}
答案 1 :(得分:0)
您的onBindViewHolder
和getItemCount
方法应使用filteredChapterList
。现在,您正在过滤列表,正确通知适配器更改,但您始终使用原始列表!
在构造函数中,您还应该更改filteredChapterList = new ArrayList<>(originalChapterList)
答案 2 :(得分:0)
et_serachList.addTextChangedListener(new TextWatcher() { @覆盖 public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @覆盖 public void onTextChanged(CharSequence s, int start, int before, int count) {
final String query = s.toString();
final ArrayList<data> filteredList = new ArrayList<>();
if (data_single.size()!=0) {
for (int i = 0; i < data_single.size(); i++) {
final String text = data_single.get(i).getMobile_no();
System.out.println("getMobile_number" + text);
if (text.contains(query)) {
filteredList.add(data_single.get(i));
}
personAdapter = new DeliverPersonAdapter(getContext(), filteredList);
recyclerView.setAdapter(personAdapter);
report_order = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(report_order);
personAdapter.notifyDataSetChanged();
}
}
}
@Override
public void afterTextChanged(Editable s) {
// filter(s.toString());
}
});