Android 实现列表搜索

RecycleView搜索过滤器-getFilter()
记录一下,最近做项目,有一个需求,在列表中需要监听输入数据来过滤列表
Android 实现列表搜索_第1张图片
Android 实现列表搜索_第2张图片
Android 提供了Fileterable类,可以通过过滤器来过滤数据。通常,getFilter()方法必须在提供过滤条件的适配器类中被重写,以通过列表进行搜索。下面是通过getFilter(),来过滤数据。

完整Adapter代码:

public class BuildingAdapter extends RecyclerView.Adapter implements Filterable {
    private List mSourceList = new ArrayList<>();
    private List mFilterList = new ArrayList<>();
    private Context mContext;
    private OnItemClickListener mListener;

    public BuildingAdapter(Context context, List list) {
        this.mContext = context;
        this.mSourceList = list;
        //这里需要初始化filterList
        mFilterList = list;
    }

    public void setListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @NonNull
    @Override
    public BuildingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BuildingHolder(LayoutInflater.from(mContext).inflate(R.layout.item_building, parent, false));
    }

    @Override
    public void onBindViewHolder(BuildingHolder holder, int position) {
        holder.tvType.setText(mContext.getString(R.string.building));
        holder.tvType.setBackgroundResource(R.drawable.bg_building);
        //这里也是过滤后的list
        holder.tvName.setText(mFilterList.get(position));
    }

    @Override
    public int getItemCount() {
        //注意这里需要是过滤后的list
        return mFilterList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String filter = charSequence.toString();
                if (TextUtils.isEmpty(filter)) {
                    //没有过滤的内容,则使用源数据
                    mFilterList = mSourceList;
                } else {
                    filter = filter.toUpperCase();//小写变大写,是大写的不变
                    List filteredList = new ArrayList<>();
                    for (String str : mSourceList) {
                        //这里根据需求,添加匹配规则
                        if (str.contains(filter)) {
                            filteredList.add(str);
                        }
                    }
                    mFilterList = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilterList;
                return filterResults;
            }

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

    class BuildingHolder extends RecyclerView.ViewHolder {
        TextView tvType, tvName;

        public BuildingHolder(@NonNull View itemView) {
            super(itemView);
            tvType = itemView.findViewById(R.id.tv_type);
            tvName = itemView.findViewById(R.id.tv_name);
            itemView.setOnClickListener(view -> {
                if (mListener != null) {
                    mListener.onItemClick(getAdapterPosition());
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}

设置EditText监听,并监听软键盘搜索按钮

etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mAdapter.getFilter().filter(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        
        etSearch.setOnEditorActionListener((textView, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                hideInput();
                mAdapter.getFilter().filter(etSearch.getText().toString());
                return true;
            }
            return false;
        });

结束!

你可能感兴趣的:(Android)