如何在RecyclerView Android中实现Filter搜索?

时间:2016-11-05 18:23:29

标签: android menu android-recyclerview

我有RecyclerView并且它的工作正常,但我需要实现过滤器搜索RecyclerView我在互联网上搜索但我无法成功,所以我希望任何人都帮我实现过滤器我的RecyclerView这是我的代码,请尽量帮助我

这是 RecyclerView 活动

    public class TodoList extends AppCompatActivity implements DialogFragUpdateListener {
    RecyclerView todoRecyclerView;
    private RecyclerView.Adapter todoAdapter;
    private RecyclerView.LayoutManager todoLayoutManager;
    public List<ToDo> results;
    public List<String> list = new ArrayList<>();
    TodoRecyclerAdapter recyclerAdapter;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_list);

        todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
        todoRecyclerView.setHasFixedSize(true);
        results= new ArrayList<ToDo>();
        todoLayoutManager = new LinearLayoutManager(this);
        todoRecyclerView.setLayoutManager(todoLayoutManager);
 getRetrofitObject();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TodoList.this,AddToDo.class);
                startActivity(intent);
            }
        });}

    public void getRetrofitObject() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);
        Call<Result> call = service.getresults();
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                results = response.body().getResults();
                todoRecyclerView.setAdapter(new TodoRecyclerAdapter(this,results));
            }
            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Log.d("onFailure", t.toString());
            }});}
 public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {

                return true;
            }

            @Override
            public boolean onQueryTextChange(String searchQuery) {

                return true;

            }
        });
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return true;
            }
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }
        });
        return true;
    }

这是适配器

public class TodoRecyclerAdapter extends RecyclerView.Adapter<TodoRecyclerAdapter.ViewHolder> {

static List<ToDo> todoResults;
static Context context;
List<String> results;
private List<ToDo> orig;
private List<ToDo> items;
private ArrayList<ToDo> arrayList = null, stringArrayList;


public TodoRecyclerAdapter(Callback<Result> callback, List<ToDo> results) {
    this.todoResults = results;
    arrayList = (ArrayList<ToDo>) results;

}



@Override
public TodoRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_items, null);
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(final TodoRecyclerAdapter.ViewHolder holder, final int position) {
    holder.todoTitle.setText(todoResults.get(position).getTODO_TITLE().toString());
    String priority = todoResults.get(position).getPriority().toString();
    if (priority.equals("Low")) {
        holder.todoImage.setImageResource(R.drawable.low);
    } else if (priority.equals("Normal")) {
        holder.todoImage.setImageResource(R.drawable.normal);
    } else if (priority.equals("High")) {
        holder.todoImage.setImageResource(R.drawable.high);
    }
}

@Override
public int getItemCount() {
    return todoResults.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView todoTitle;
    public ImageView todoImage;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        todoTitle = (TextView) itemLayoutView.findViewById(R.id.todo_title);
        todoImage = (ImageView) itemLayoutView.findViewById(R.id.imageView2);
    }
}

请尝试帮助我,因为此方法来自我的应用程序中的主要功能

2 个答案:

答案 0 :(得分:0)

您可以在onQueryTextChange中使用your_adapter.getFilter()。filter(searchQuery)。

答案 1 :(得分:0)

试 您的TodoList活动类中的recyclerAdapter.getFilter()。filter(searchQuery)

但是如果你想做这样的事情 Action bar search