您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android中使用RecyclerView添加搜索過濾器,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
RecycleView搜索過濾器-getFilter()
Android 提供了Fileterable類,可以通過過濾器(條件)來過濾數(shù)據(jù)。通常,getFilter()方法必須在提供過濾條件的適配器類中被重寫,以通過列表進(jìn)行搜索。下面是通過getFilter(),來過濾數(shù)據(jù)。
@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) { //這里根據(jù)需求,添加匹配規(guī)則 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; //刷新數(shù)據(jù) notifyDataSetChanged(); } }; }
完整Adapter代碼:
//這里實現(xiàn)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() { //執(zhí)行過濾操作 @Override protected FilterResults performFiltering(CharSequence charSequence) { String charString = charSequence.toString(); if (charString.isEmpty()) { //沒有過濾的內(nèi)容,則使用源數(shù)據(jù) mFilterList = mSourceList; } else { List<String> filteredList = new ArrayList<>(); for (String str : mSourceList) { //這里根據(jù)需求,添加匹配規(guī)則 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); } } }
數(shù)據(jù)初始化部分
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]); } }
設(shè)置RecycleView和EditText監(jiān)聽
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) { } });
上述內(nèi)容就是怎么在Android中使用RecyclerView添加搜索過濾器,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。