SearchView

时间:2017-11-30 18:44:40

标签: android android-recyclerview

我正在使用RecyclerView并希望用它实现SearchView。我从早上开始工作,但是我无法按照自己的意愿实施它。

问题: - 当我在RecyclerView中搜索任何项目时,然后在我尝试搜索另一个项目之后正确更新列表,然后列表变空。我想通过键盘按键自动更新我的列表。 我知道在SOF上有很多关于它的问题,但我主要尝试了所有这些问题而且我不理解他们的代码。

所有这些答案都表明我们应该清除列表对象,但是当任何人搜索任何项目时我都无法弄清楚如何取回原始列表。

有我的Adapter类: -

public class SongAdapter extends RecyclerView.Adapter<SongViewHolder> implements Filterable{
    private Context context;
    private List<SongObject> allSongs;
    MyEditText options;
    SongViewHolder songViewHolder;
    View view;
    PlaylistCounterDataSource dataSource;
    DataSource fdataSource;
    private List<SongObject> mFilteredList;
    public SongAdapter(Context context, List<SongObject> allSongs) {
        this.context = context;
        this.allSongs = allSongs;
    }
    @Override
    public SongViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.song_list_layout, parent, false);

        options = view.findViewById(R.id.options);
        return new SongViewHolder(view);
    }
    @Override
    public void onBindViewHolder(final SongViewHolder holder, final int position) {
        songViewHolder = holder;
        holder.setIsRecyclable(false);

        options.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showOptions(view, position);
                notifyDataSetChanged();
            }
        });
        SongObject songs = allSongs.get(position);
        holder.songTitle.setText(songs.getSongTitle());
        holder.songAuthor.setText(songs.getSongAuthor());
        Glide.with(context)
                .load(songs.getSongCover())
                .asBitmap()
                .placeholder(R.drawable.default_art)
                .error(R.drawable.default_art)
                .override(200,200)
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.songImage);
    }
 @Override
    public int getItemCount() {
        return allSongs.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    mFilteredList = allSongs;
                }else {
                    List<SongObject> filteredList = new ArrayList<>();
                    for (SongObject androidVersion : allSongs) {

                        if (androidVersion.getSongTitle().toLowerCase().contains(charString) || androidVersion.getSongAuthor().toLowerCase().contains(charString)) {

                            filteredList.add(androidVersion);

                        }
                    }
                    mFilteredList = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilteredList;
                Log.d("filter",""+filterResults.values);
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                allSongs = (ArrayList<SongObject>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
}

这是片段类: -

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.music_search, menu);
        MenuItem searchItem = menu.findItem(R.id.music_search_bar);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setQueryHint("Search Song");
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {

                mAdapter.getFilter().filter(s);

            /*   s = s.toLowerCase();
               ArrayList<SongObject> objects = new ArrayList<>();
               for(SongObject so : objects){
                   String name = so.getSongTitle().toLowerCase();
                   if(name.contains(s))
                       objects.add(so);
               }
               mAdapter.filterList(objects);*/

                return true;
            }

        super.onCreateOptionsMenu(menu, inflater);
    }

在某些答案中,他们说当搜索字符串的长度等于0时重新生成recyclelerView列表。但如果有更多项目具有相同的起始字母,会发生什么: - ABC 加 ADU 它永远不会等于0。

1 个答案:

答案 0 :(得分:2)

     // this is wrong
@Override
    public int getItemCount() {
        return allSongs.size();
    }

// use this 
@Override
    public int getItemCount() {
        return filteredList.size();
    }

在初始状态下将所有歌曲添加到filteredSongs列表中,然后当用户查询清除filteredList并将结果添加到其中时