1. 程式人生 > >AutoCompleteTextView輸入框自動提示功能

AutoCompleteTextView輸入框自動提示功能

public class SuggestPoiAdapter extends BaseAdapter implements Filterable {
    private static final int ITEM_BAIDU = 0;
    private static final int CLOSE_ITEM = 1;
    private Context mContext;
    private ArrayList<SuggestionInfo> data;
    private ArrayFilter mFilter;
    private ArrayList<SuggestionInfo> mBaiduData;
    private OnCloseListener onCloseListener;
    private OnItemClickListener onItemClickListener;


    public SuggestPoiAdapter(Context context, ArrayList<SuggestionInfo> suggestionInfos) {
        this.mContext = context;
        this.data = suggestionInfos;
    }

    public void setOnCloseListener(OnCloseListener onCloseListener) {
        this.onCloseListener = onCloseListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        } else if (data.size() > 6) {
            return 7;
        } else if (data.size() > 0) {
            return data.size() + 1;
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }


    @Override
    public int getItemViewType(int position) {
        int count;
        if (data.size() >= 6) {
            count = 6;
        } else {
            count = data.size();
        }
        if (position < count) {
            return ITEM_BAIDU;
        } else {
            return CLOSE_ITEM;  
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        BaiDuViewHolder baiDuViewHolder = null;
        CloseViewHolder closeViewHolder = null;
        if (convertView == null) {
            switch (itemViewType) {
                case ITEM_BAIDU:
                    convertView = View.inflate(mContext, R.layout.item_baidu, null);
                    baiDuViewHolder = new BaiDuViewHolder();
                    baiDuViewHolder.baiduAdrr = (TextView) convertView.findViewById(R.id.tv_address); // 列表樣式
                    convertView.setTag(baiDuViewHolder);
                    break;
                case CLOSE_ITEM:
                    convertView = View.inflate(mContext, R.layout.item_close, null);
                    closeViewHolder = new CloseViewHolder();
                    closeViewHolder.close = (TextView) convertView.findViewById(R.id.tv_close); // 關閉按鈕
                    convertView.setTag(closeViewHolder);
                    break;
            }
        } else {
            switch (itemViewType) {
                case ITEM_BAIDU:
                    baiDuViewHolder = (BaiDuViewHolder) convertView.getTag();
                    break;
                case CLOSE_ITEM:
                    closeViewHolder = (CloseViewHolder) convertView.getTag();
                    break;
            }
        }

        switch (itemViewType) {
            case ITEM_BAIDU:
                String name = data.get(position).getKey();
                baiDuViewHolder.baiduAdrr.setText(name);
                baiDuViewHolder.baiduAdrr.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onClickListener(position, data);
                        }
                    }
                });
                break;
            case CLOSE_ITEM:
                closeViewHolder.close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onCloseListener != null) {
                            onCloseListener.close();
                        }
                    }
                });
                break;
        }
        return convertView;
    }

    public void setData(ArrayList<SuggestionInfo> allPoi) {
        if (allPoi != null) {
            if (data != null) {
                data.clear();
            }
            data.addAll(allPoi);
        }
    }


    class BaiDuViewHolder {
        TextView baiduAdrr;
    }

    class CloseViewHolder {
        TextView close;
    }

    public interface OnCloseListener {
        void close();
    }

    public interface OnItemClickListener {
        void onClickListener(int position, ArrayList<SuggestionInfo> data);
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    /**
     * 實現其中的兩個方法,第一方法時資料的過濾邏輯,第二個方法是把過濾結果賦值給資料來源。
     */
    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            //如果沒有過濾條件則不過濾
            if (mBaiduData == null) {
                mBaiduData = new ArrayList<SuggestionInfo>(data);
            }
            //如果沒有過濾條件則不過濾
            if (prefix == null || prefix.length() == 0) {
                results.values = mBaiduData;
                results.count = mBaiduData.size();
            } else {
                //過濾條件
                String prefixString = prefix.toString().toLowerCase();
                ArrayList<SuggestionInfo> unfilteredValues = mBaiduData;
                int count = unfilteredValues.size();

                ArrayList<SuggestionInfo> newValues = new ArrayList<SuggestionInfo>(count);
                //迴圈變數資料來源,如果有屬性滿足過濾條件,則新增到result中
                for (int i = 0; i < count; i++) {
                    SuggestionInfo pc = unfilteredValues.get(i);
                    if (pc != null) {
                        if (pc.getKey().contains(prefixString)) {
                            newValues.add(pc);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        //在這裡返回過濾結果
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
//          notifyDataSetInvalidated(),會重繪控制元件(還原到初始狀態)
//          notifyDataSetChanged(),重繪當前可見區域
            //noinspection unchecked
            data = (ArrayList<SuggestionInfo>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

}