我正在尝试在recyclerview
中添加搜索功能。我在google上找到了一个教程并实现了相同的功能。使用recyclerview
中的editText进行搜索。但是当我输入输入时,我的列表消失了。我哪里错了?我的Recycler视图包含多个文本框。以下是MyAdapter class
。
如何使用以下代码实现搜索功能?
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>implements Filterable {
private List<ListItem> listItems;
private List<ListItem> mFilterData;
private Context context;
String m,e;
private ItemFilter mFilter = new ItemFilter();
public MyAdapter(List<ListItem> itemList, Context context) {
this.listItems = itemList;
this.context = context;
this.mFilterData = listItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem listItem = mFilterData.get(position);
holder.nameText.setText(listItem.getName());
holder.addText.setText(listItem.getAddress());
Picasso.with(context).load(listItem.getLogo()).resize(180,80).into(holder.imageText);
}
@Override
public int getItemCount() {
return mFilterData.size();
}
public int getCount() {
return mFilterData.size();
}
public ListItem getItem(int position) {
return mFilterData.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String userString = constraint.toString().toLowerCase();
FilterResults filterResults = new FilterResults();
final List<ListItem> originalList = listItems;
int count = originalList.size();
final ArrayList<ListItem> resultList = new ArrayList<>(count);
ListItem tDescription;
for (int i = 0; i < count; i++) {
tDescription= originalList.get(i);
if (tDescription.getName().toLowerCase().contains(userString)) {
resultList.add(tDescription);
}
}
filterResults.values = resultList;
filterResults.count = resultList.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilterData = (ArrayList<ListItem>) results.values;
notifyDataSetChanged();
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView nameText, addText,mobText,emailText;
public ImageView imageText;
public ViewHolder(View itemView) {
super(itemView);
//code
}
}
}
搜寻
private void addTextListener() {
search.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
s = s.toString().toLowerCase();
final List<ListItem> filteredList = new ArrayList<>();
for (int i = 0; i < listItems.size(); i++) {
final String text = listItems.get(i).toString();
if (text.contains(s)) {
filteredList.add(listItems.get(i));
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new MyAdapter(filteredList, getActivity());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged(); // data set changed
}
});
}
答案 0 :(得分:0)
尝试以下代码
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String userString = constraint.toString().toLowerCase();
if (userString.isEmpty()) {
mFilterData = listItems;
} else {
ArrayList<ListItem> resultList = new ArrayList<>();
for (ListItem mListItem : listItems) {
if (mListItem.getName().toLowerCase().contains(userString)) {
resultList.add(mListItem);
}
}
mFilterData = resultList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilterData;
filterResults.count = mFilterData.size();
return filterResults;
}
}
更新
从edittext OnTextChange中删除所有代码并写下面的代码,不需要设置适配器,只需在下面写一下oneline代码并删除其余代码
adapter.getFilter().filter(s.toString());