您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android中使用RecyclerView添加搜索過濾器,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
RecycleView搜索過濾器-getFilter()
Android 提供了Fileterable類,可以通過過濾器(條件)來過濾數據。通常,getFilter()方法必須在提供過濾條件的適配器類中被重寫,以通過列表進行搜索。下面是通過getFilter(),來過濾數據。
@Override public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { String charString = charSequence.toString(); if (charString.isEmpty()) { mFilterList = mSourceList; } else { List<String> filteredList = new ArrayList<>(); for (String str : mSourceList) { //這里根據需求,添加匹配規則 if (str.contains(charString)) { filteredList.add(str); } } mFilterList = filteredList; } FilterResults filterResults = new FilterResults(); filterResults.values = mFilterList; return filterResults; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { mFilterList = (ArrayList<String>) filterResults.values; //刷新數據 notifyDataSetChanged(); } }; }
完整Adapter代碼:
//這里實現Filterable接口 class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.TitleHolder> implements Filterable { private List<String> mSourceList = new ArrayList<>(); private List<String> mFilterList = new ArrayList<>(); private void appendList(List<String> list) { mSourceList = list; //這里需要初始化filterList mFilterList = list; } @Override public TitleHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new TitleHolder(LayoutInflater.from(FastScrollRecyclerActivity.this).inflate(R.layout.item_textview_view, parent, false)); } @Override public void onBindViewHolder(TitleHolder holder, int position) { //這里也是過濾后的list holder.tv.setText(mFilterList.get(position)); } @Override public int getItemCount() { //注意這里需要是過濾后的list return mFilterList.size(); } //重寫getFilter()方法 @Override public Filter getFilter() { return new Filter() { //執行過濾操作 @Override protected FilterResults performFiltering(CharSequence charSequence) { String charString = charSequence.toString(); if (charString.isEmpty()) { //沒有過濾的內容,則使用源數據 mFilterList = mSourceList; } else { List<String> filteredList = new ArrayList<>(); for (String str : mSourceList) { //這里根據需求,添加匹配規則 if (str.contains(charString)) { filteredList.add(str); } } mFilterList = filteredList; } FilterResults filterResults = new FilterResults(); filterResults.values = mFilterList; return filterResults; } //把過濾后的值返回出來 @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { mFilterList = (ArrayList<String>) filterResults.values; notifyDataSetChanged(); } }; } class TitleHolder extends RecyclerView.ViewHolder { TextView tv; public TitleHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.id_tv_test); } } }
數據初始化部分
private String[] str = new String[]{ "apple", "apple juice", "apple pie", "abalone", "bread", "brandy", "Blueberry", "Banana", "chocolate", "cake", "chicken", "cheese", "Durian", "Dim Sam", "Dumpling", "duck", "egg", "English muffin", "eggplant", "French toast", "fish", "fig", "fruit" };
private List<String> mList = new ArrayList<>(); private void initList() { for (int i = 0; i < str.length; i++) { mList.add(str[i]); } }
設置RecycleView和EditText監聽
RecyclerView rv = (RecyclerView) findViewById(R.id.id_rv); LinearLayoutManager manager = new LinearLayoutManager(this); rv.setLayoutManager(manager); final TitleAdapter adapter = new TitleAdapter(); adapter.appendList(mList); rv.setAdapter(adapter); EditText et = findViewById(R.id.id_et); et.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence sequence, int i, int i1, int i2) { adapter.getFilter().filter(sequence.toString()); } @Override public void afterTextChanged(Editable editable) { } });
上述內容就是怎么在Android中使用RecyclerView添加搜索過濾器,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。