Android - 为 RecyclerView 实现搜索过滤器 [英] Android - Implementing search filter to a RecyclerView
本文介绍了Android - 为 RecyclerView 实现搜索过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的 RecyclerView
中实现基于 EditText
的搜索功能.输入时必须过滤列表.
I need to implement a search feature based on an EditText
to my RecyclerView
. The list must be filtered while typing.
代码如下:
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 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;
}
推荐答案
在您的适配器中添加用于更新列表的新功能
in your adapter add new function for update the list
public void updateList(List<DataHolder> list){
displayedList = list;
notifyDataSetChanged();
}
为搜索添加 textWatcher 假设您使用 Edittext 作为搜索字段
add textWatcher for search lets say you are using Edittext as search field
searchField.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// filter your list from your input
filter(s.toString());
//you can use runnable postDelayed like 500 ms to delay search text
}
});
简单的过滤功能
void filter(String text){
List<DataHolder> temp = new ArrayList();
for(DataHolder d: displayedList){
//or use .equal(text) with you want equal match
//use .toLowerCase() for better matches
if(d.getEnglish().contains(text)){
temp.add(d);
}
}
//update recyclerview
disp_adapter.updateList(temp);
}
使用Butterknife
@OnTextChanged(R.id.feature_manager_search)
protected void onTextChanged(CharSequence text) {
filter(text.toString());
}
这篇关于Android - 为 RecyclerView 实现搜索过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文