How to filter a RecyclerView with a SearchView

后端 未结 11 1714
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:28

I am trying to implement the SearchView from the support library. I want the user to be to use the SearchView to filter a List of movi

11条回答
  •  醉话见心
    2020-11-22 00:46

    In Adapter:

    public void setFilter(List newList){
            mChannels = new ArrayList<>();
            mChannels.addAll(newList);
            notifyDataSetChanged();
        }
    

    In Activity:

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    newText = newText.toLowerCase();
                    ArrayList newList = new ArrayList<>();
                    for (Channel channel: channels){
                        String channelName = channel.getmChannelName().toLowerCase();
                        if (channelName.contains(newText)){
                            newList.add(channel);
                        }
                    }
                    mAdapter.setFilter(newList);
                    return true;
                }
            });
    

提交回复
热议问题