ecyclerView列表项使用实现可过滤的EditText的搜

ecyclerView列表项使用实现可过滤的EditText的搜

本文介绍了RecyclerView列表项使用实现可过滤的EditText的搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SearchListAdapter.java

使用EditText实现可过滤的RecyclerView列表搜索不起作用.请检查此代码.在我的情况下,列表已被过滤,但是由于哪个列表相同,recyclerView适配器未更改.

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();
        }
    }
}

推荐答案

最后完成了,但是使用了另一种方式,无法实现更合适的方式.

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


        }

这篇关于RecyclerView列表项使用实现可过滤的EditText的搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:27