我需要基於一個EditText我RecyclerView必須過濾列表,以實現搜索功能,同時typngAndroid的 - 實現搜索過濾器的RecyclerView
下面的代碼:
DisplayAdapter.java這是適配器
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 norse) {
this.english = english;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
}
和這裏的,我一起工作的片段:
@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;
}
'if(d.getEnglish()。equal(text)){ temp.add(d); (d.getEnglish()。contains(text)){ }'change to'if(d.getEnglish()。contains(text))temp.add(d); }' –
感謝兄弟工作正常d.getName()。toLowerCase()。contains(text.toLowerCase())將得到明確結果感謝 – Sam
該方法適用於感謝 –