Android输入框实时模糊搜索效果的示例代码

所属分类: 软件编程 / Android 阅读数: 80
收藏 0 赞 0 分享

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;
   }
更多精彩内容其他人还在看

Android 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多