Android输入框实时模糊搜索_xamarin autocompletetextview 模糊-程序员宅基地

技术标签: 过滤器  android  Android  

Android输入框实时模糊搜索

很多开发场景会用到搜索框实时模糊搜索来帮助用户输入内容,如图

模糊搜索效果

思路是在EditText 字符变动的时候 弹出ListPopupwindow并更新列表,这样的做法google已经封装为AutoCompleteTextView

用法

        mAutoCompleteTextView.setAdapter(adapter);
        mAutoCompleteTextView.setFocusable(true);
        mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            
            }
        });

adapter自定义
Adapter 继承 BaseApdater 需要实现 Filterable 接口

private class SearchAdapter extends BaseAdapter implements Filterable {
    

        private Context mContext;

        public SearchAdapter(Context context) {
    
            super();
            this.mContext = context;
        }

        @Override
        public int getCount() {
    
            if (mSearchCustomEntities == null) {
    
                return 0;
            } else {
    
                return mSearchCustomEntities.size();
            }
        }

        @Override
        public Object getItem(int position) {
    
            if (mSearchCustomEntities == null) {
    
                return null;
            } else {
    
                return mSearchCustomEntities.get(position);
            }

        }

        @Override
        public long getItemId(int position) {
    
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
            if (convertView == null) {
    
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_search_custom, null, false);
                holder.tag = (TextView) convertView.findViewById(R.id.tv_custome_type);
                holder.name = (TextView) convertView.findViewById(R.id.custom_name);
                holder.phone = (TextView) convertView.findViewById(R.id.tv_phone);
                convertView.setTag(holder);
            } else {
    
                holder = (ViewHolder) convertView.getTag();
            }
            holder.phone.setText(mSearchCustomEntities.get(position).phone);
            holder.name.setText(mSearchCustomEntities.get(position).name);
            if (mSearchCustomEntities.get(position).type == CustomerType.TEMPORARY_CUSTOMER.getType()) {
    
                holder.tag.setVisibility(View.VISIBLE);
                holder.tag.setText(mContext.getString(R.string.tag_temp));
                holder.tag.setTextColor(mContext.getResources().getColor(R.color.customer_temp_txt));
                holder.tag.setBackground(mContext.getResources().getDrawable(R.drawable.bg_solid_quote_type_inner_temp));
            } else if (mSearchCustomEntities.get(position).type == CustomerType.COLLECTIVE_UNIT.getType()) {
    
                holder.tag.setVisibility(View.VISIBLE);
                holder.tag.setText(mContext.getString(R.string.tag_unit));
                holder.tag.setTextColor(mContext.getResources().getColor(R.color.customer_unit_txt));
                holder.tag.setBackground(mContext.getResources().getDrawable(R.drawable.bg_solid_quote_type_inner_unit));
            } else if (mSearchCustomEntities.get(position).type == CustomerType.OUTER_MOTORCADE.getType()) {
    
                holder.tag.setVisibility(View.VISIBLE);
                holder.tag.setText(mContext.getString(R.string.tag_car));
                holder.tag.setTextColor(mContext.getResources().getColor(R.color.customer_car_txt));
                holder.tag.setBackground(mContext.getResources().getDrawable(R.drawable.bg_solid_quote_type_inner_car));
            } else {
    
                holder.tag.setVisibility(View.GONE);
            }

            return convertView;
        }

        @Override
        public Filter getFilter() {
    
            if (mFilter == null) {
    
                mFilter = new ArrayFilter();
            }
            return mFilter;
        }
          private class ViewHolder {
    
            TextView tag;
            TextView name;
            TextView phone;
        }

自定义 过滤器

  private class ArrayFilter extends Filter {
    

            @Override
            protected FilterResults performFiltering(CharSequence prefix) {
    
                FilterResults results = new FilterResults();
                String prefixString = prefix.toString();


                //筛选部分  访问接口
  
                XbcClient.getCustomList(prefixString, new EntitiesObserver<SearchCustomEntity>() {
    
                    @Override
                    protected void onGot(List<SearchCustomEntity> entities, String msg, int errCode) {
    
                        if (entities != null && entities.size() > 0) {
    
                            mSearchCustomEntities.clear();
                            mSearchCustomEntities.addAll(entities);
                            mSearchAdapter.notifyDataSetChanged();
                        }else{
    
                            if (mSearchCustomEntities!=null & mSearchCustomEntities.size()>0) {
    
                                mSearchCustomEntities.clear();
                                mSearchAdapter.notifyDataSetInvalidated();
                            }
                        }
                    }
                });

                results.values = mSearchCustomEntities;
                results.count = mSearchCustomEntities.size();
                return results;
            }
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35892584/article/details/107781526

智能推荐

前端实现打印预览功能以及page-break-inside属性解决打印换行问题(打印预览表格或文字被分割开)-程序员宅基地

文章浏览阅读1.4w次,点赞4次,收藏26次。遇到的问题:打印预览的时候表格被分割了,就是一共两页而其中一行显示在不同的两个页面。如下图:_page-break-inside

稀疏矩阵在matalb中_spconvert函数-程序员宅基地

文章浏览阅读298次。1.存储2.转化为稀疏矩阵 sparse函数和full函数3.直接建立稀疏矩阵 spconvert函数4.带状稀疏矩阵spdiags函数例题:求解三对角线方程组的解_spconvert函数

iOS 开发 关闭黑暗模式_ios开发 关闭对黑暗模式的支持-程序员宅基地

文章浏览阅读1.9k次。在info.plist文件中加一条Appearance - Light, 如下图:参考博客:iOS暗黑(dark)模式适配IOS 开发之关闭暗黑模式_ios开发 关闭对黑暗模式的支持

【iOS开发】SwiftLint——代码规范工具-程序员宅基地

文章浏览阅读1.1k次。目的包括PMS及以后的Swift项目在多人开发中,即使有官网的规范模板,每个人的代码风格和规范难以做到完全一致,对后期项目维护会增加一定的困难。使用SwiftLint可以对规范代码有很大帮助。SwiftLint是啥SwiftLint是Realm公司开发的一个插件,专门用于管理Swift代码的规范,能够对原有的代码自动格式化,在 Xcode 中执行编译时,SwiftLint 会自动运行检查,不符合规范的代码会通过警告或者报错的形式指示出来,并且拥有丰富的配置项,可以进行大量的自定义规范操作,是一个很方_swiftlint

tomcat 源码阅读步骤一_myeclipse怎么查看tomcat源码-程序员宅基地

文章浏览阅读618次。tomcat源码阅读1org.apache.catalina 包包内接口主要有:AuthenticatorAuthenticator 是一个组件(通常是一个阀门或容器),它提供了这类服务的身份验证Cluster一个Cluster 像一个当地客户服务器集群那样工作,它的实现需要支持集群内的多种交流方式Contained它是一个解耦接_myeclipse怎么查看tomcat源码

[Android]APP多域名服务高可用方案_一个app要连多个域名灾备吗-程序员宅基地

文章浏览阅读2.6k次。负责公司的基础数据扫描采集.这部分对于系统的可用性基本是100%所以做了很多高可用的方案前置准备在对APP进行高可用实施之前,我们需要准备:1.核心域名多个降级[一主多备]最基础的要求,必须!!!能够支持云端下发 & 本地动态切换(蓝-绿发布 & 灰度 & UAT)2.多个CDN每个域名 都使用不同CDN,避免因CDN节点故障导致服务不可用(出现过因CDN节点异常的生产故障)3.多个部署网络机房每个域名 部署在不同地域网络机房(出现过主干线因施工被挖断的生产._一个app要连多个域名灾备吗

随便推点

pdf转word用python轻松搞定_使用Python将PDF转化为word-程序员宅基地

文章浏览阅读1.6k次。60行Python代码,实现多线程PDF转Word分解任务把PDF转为Word,分几步?两步,第一步读取PDF文件,第二步写入Word文件。是的,就是这么简单,借助Python第三方包,可以轻松实现上面两个过程,我们要用到pdfminer3k和python-docx这两个包读取PDFfrom pdfminer.pdfinterp import PDFResourceManagerfrom pdfm..._python写pdf与word互转代码

fmea手册_新版FMEA打分怎么破?(详细收录手册标准对照表...-程序员宅基地

文章浏览阅读5.6k次。新版FMEA终于正式发布了!关于新版FMEA正式版与草稿版的差异,后续黄老师将会每周撰写新文来给大家做解读及分享,敬请持续关注公众号(首页菜单的历史文章中有FMEA合集,大家可随时点击阅读旧文)。为了方便部分已经懂了七步法的学员可以直接进行新版FMEA的使用,本篇特别将新版FMEA手册中的评分表整理出来,方便参考使用。本篇文章建议收藏,后续可持续做为工具书随时参考使用。关于打分说起FMEA,打分是..._fmea打分标准对照表

Fantastical 2 for Mac(日历管理软件) v2.5免激活版-程序员宅基地

文章浏览阅读3.3k次。今天小编为您带来Fantastical 2 Mac一款易于使用的日历管理软件,Fantastical 2 Mac版采用了全新的设计风格,和Yosemite系统十分贴合,并且提供了「光」和“黑暗”两种配色模式,可以切换左栏的颜色。右侧的布局和系统原生日历十分相像,而在左侧则显示了该月日期及行程安排,并且还整合了系统原生的 「提醒事项」 。Fantastical 2Mac破解版使用说明下载完成...

微信小程序|自定义弹窗组件_微信小程序的自定义弹窗-程序员宅基地

文章浏览阅读1.3w次,点赞63次,收藏66次。深入探讨如何创建自定义弹出组件,穿插案例研究和实际应用,在实际开发中更好地利用自定义弹出组件来提升用户体验。_微信小程序的自定义弹窗

Teechart for .net 使用总结_使用teechart.dll会不会弹出收费-程序员宅基地

文章浏览阅读6.1k次。先说一下:Teechart for .net v3.0 破解版 下载地址:http://download.csdn.net/source/1565607先来张示例图:最近公司开发web图标,采用了Teechart 控件。挺好用的。 因为以前公司用这个控件写c/s程序(主要是Delphi的)很好用,所以也推荐我用这个的.net版本。经过几天的研究,用这还可以,功能很强大。总结te_使用teechart.dll会不会弹出收费

bochs_error: flat file is not writable-程序员宅基地

文章浏览阅读144次。一般这种不可写入都是权限问题,一看工作目录才发现自己在。目录下,不由得大吃一惊,赶紧使用命令。Windows的wsl环境下,使用。创建软盘时,结尾有报错。,然后就恢复正常了。_error: flat file is not writable

推荐文章

热门文章

相关标签