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

AppCompatAutoCompleteTextView自动补全, 实时动态获取网络数据

程序员文章站 2022-07-04 12:34:47
1.添加AppCompatAutoCompleteTextView内容变化监听并设置自定的Adapter autoComplete.setAdapter(autoCompleteAdapter);autoComplete.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after)...

1.初始化AppCompatAutoCompleteTextView

 autoComplete.setAdapter(autoCompleteAdapter);
//item点击监听
autoCompleteAdapter.setOnItemClickListener(new AutoCompleteAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(String aaa) {

                autoComplete.setText(aaa);
                autoComplete.setSelection(aaa.length());//光标移动到末尾
            }
        });
   //内容变化监听
autoComplete.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (触发条件) {
          网络获取搜索结果
        }

    }
});

2.自定义Adapter(需要继承BaseAdapter并实现Filterable(筛选类))

public class AutoCompleteAdapter extends BaseAdapter implements Filterable {


    private List<String> stringList;
    private Context context;

    private OnItemClickListener onItemClickListener;

    public AutoCompleteAdapter(Context context) {
        this.context = context;
    }
     //更新数据
    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
        notifyDataSetChanged();
    }
    //设置item条目点击监听
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return stringList != null ? stringList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return stringList != null ? stringList.get(position) : null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(android.R.id.text1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(stringList.get(position));
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.OnItemClick(stringList.get(position));
            }
        });

        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<String> newData = new ArrayList<>();
                if (stringList != null && stringList.size() != 0) {
                    newData.addAll(stringList);
                }
                results.values = newData;
                results.count = newData.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                stringList = (ArrayList) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }

    class ViewHolder {
        private TextView textView;
    }

    public interface OnItemClickListener {
        void OnItemClick(String aaa);
    }


}

 

本文地址:https://blog.csdn.net/qq_22007319/article/details/110633259