91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中實現輸入框實時模糊搜索效果的方法

發布時間:2020-08-13 16:27:11 來源:億速云 閱讀:905 作者:小新 欄目:開發技術

Android中實現輸入框實時模糊搜索效果的方法?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!

Android輸入框實時模糊搜索

很多開發場景會用到搜索框實時模糊搜索來幫助用戶輸入內容,如圖

Android中實現輸入框實時模糊搜索效果的方法

思路是在EditText 字符變動的時候 彈出ListPopupwindow并更新列表,這樣的做法google已經封裝為AutoCompleteTextView

用法

mAutoCompleteTextView.setAdapter(adapter);
  mAutoCompleteTextView.setFocusable(true);
  mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<&#63;> 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中實現輸入框實時模糊搜索效果的方法大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

虹口区| 定州市| 仙居县| 临海市| 外汇| 洛浦县| 临高县| 镇安县| 嘉鱼县| 曲周县| 乡城县| 鄂托克旗| 华坪县| 石门县| 长治市| 宜阳县| 塘沽区| 平泉县| 江源县| 郯城县| 新田县| 黑水县| 宁陕县| 龙陵县| 三台县| 龙门县| 临泉县| 遂昌县| 阿巴嘎旗| 宜兰市| 璧山县| 开封县| 舒兰市| 丰宁| 连平县| 闽侯县| 隆德县| 来凤县| 科尔| 农安县| 方城县|