欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-01 13:07:35
android输入框实时模糊搜索很多开发场景会用到搜索框实时模糊搜索来帮助用户输入内容,如图思路是在edittext 字符变动的时候 弹出listpopupwindow并更新列表,这样的做法googl...

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;
   }

总结

到此这篇关于android输入框实时模糊搜索效果的示例代码的文章就介绍到这了,更多相关android输入框实时模糊搜索内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!