RecyclerView List items Search using EditText impl

2019-09-18 01:17发布

SearchListAdapter.java

RecyclerView List Search using EditText implementing filterable do not work. Please check out this code. Here in my case the list is filtered but recyclerView adapter is not changed due to which list remains same.

/**
 * Created by Dell on 4/8/2016.
 */
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> implements Filterable {

    private Context context;
    public static ArrayList<ChapterListModel> chapterList;

    private List<ChapterListModel> orignalChapterList = new ArrayList<>();
    private final List<ChapterListModel> filteredChapterList;

    public SearchListAdapter(Context context, ArrayList<ChapterListModel> chapterList) {
        this.context = context;
        this.chapterList = chapterList;
        orignalChapterList = chapterList;
        filteredChapterList = new ArrayList<>();

    }


    @Override
    public SearchListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_chapter_list, parent, false)
        );
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        holder.title.setText(chapterList.get(position).getTitle());
    }


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

    @Override
    public Filter getFilter() {
        return new CustomFilter(this, orignalChapterList);
    }

    public class ViewHolder extends RecyclerView.ViewHolder  {

        TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.textView);
        }
    }

    public class CustomFilter extends Filter {
        private SearchListAdapter adapter;
        private final List<ChapterListModel> originalList;
        private final List<ChapterListModel> filteredList;

        private CustomFilter(SearchListAdapter adapter, List<ChapterListModel> originalList) {
            super();
            this.adapter = adapter;
            this.originalList = new LinkedList<>(originalList);
            this.filteredList = new ArrayList<>();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            filteredList.clear();
            final FilterResults results = new FilterResults();

            if (constraint.length() == 0) {
                filteredList.addAll(originalList);
            } else {
                final String filterPattern = constraint.toString().toLowerCase().trim();

                for (final ChapterListModel chapter : originalList) {
                    if (chapter.getTitle().contains(filterPattern)) {
                        filteredList.add(chapter);
                    }
                }
            }
            results.values = filteredList;
            results.count = filteredList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            adapter.filteredChapterList.clear();
            adapter.filteredChapterList.addAll((List<ChapterListModel>) results.values);
            adapter.notifyDataSetChanged();
        }
    }
}

2条回答
对你真心纯属浪费
2楼-- · 2019-09-18 01:45

Your onBindViewHolder and getItemCount methods should be using the filteredChapterList. Right now, you are filtering your list, correctly notifying the adapter of the change, but you're always using the original list!

In your constructor, you should also change filteredChapterList = new ArrayList<>(originalChapterList)

查看更多
Animai°情兽
3楼-- · 2019-09-18 01:52

Finally done but using another way, not implementing fiterable.

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {


            final String query = s.toString().toLowerCase().trim();
            final ArrayList<ChapterListModel> filteredList = new ArrayList<>();

            for (int i = 0; i < chapterLists.size(); i++) {

                final String text = chapterLists.get(i).getTitle().toLowerCase();
                if (text.contains(query)) {
                    filteredList.add(chapterLists.get(i));
                }
            }

            recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
            adapter = new SearchListAdapter(SearchActivity.this, filteredList);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();  // data set changed


        }
查看更多
登录 后发表回答