I need to implement a search feature based on an EditText
to my RecyclerView
. The list must be filtered while typing.
Here's the code :
DisplayAdapter.java this is the adapter
public class DisplayAdapter extends RecyclerView.Adapter<DisplayAdapter.MyViewHolder>{
private List<DataHolder> displayedList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView english_d;
public MyViewHolder(View view){
super(view);
english_d = (TextView) view.findViewById(R.id.engword);
}
}
public DisplayAdapter(List<DataHolder> displayedList){
this.displayedList = displayedList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
// create a layout
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
DataHolder content = displayedList.get(position);
holder.english_d.setText(content.getEnglish());
}
@Override
public int getItemCount() {
return displayedList.size();
}
}
DataHolder.java
public class DataHolder {
private String english;
public DataHolder() {
}
public DataHolder(String english, String german) {
this.english = english;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
}
and here's the Fragment that I am working with :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_two_fragment, container, false);
rv = (RecyclerView) view.findViewById(R.id.list_view_english);
rv.setHasFixedSize(true);
inputSearch = (EditText) view.findViewById(R.id.inputSearch);
rv.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);
DisplayContent();
disp_adapter = new DisplayAdapter(displayedList);
rv.setAdapter(disp_adapter);
return view;
}
question from:
https://stackoverflow.com/questions/40754174/android-implementing-search-filter-to-a-recyclerview 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…