最近项目中需要做搜索功能,实现类似 Google、Baidu 搜索的 下拉提示效果。android为我们提供了 AutoCompleteTextView 控件来完成此功能。
网上好多例子都是简单使用 ArrayAdapter 来实现的,界面比较简单,实际项目中用处不大;自己研究了一番,自定义Adapter 继承BaseAdapter 并实现Filterable 接口 实现了上述功能。
运行效果截图
package com.example.actv;
import java.util.ArrayList;
import java.util.List;
import com.example.actv.entity.PhoneContact;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity implements OnItemClickListener {
List mList;
private AutoCompleteTextView mACTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildAppData();
findView();
}
private void buildAppData() {
String[] names = { "abc", "allen", "bird", "bike", "book", "cray",
"david", "demon", "eclipse", "felling", "frank", "google",
"green", "hill", "hook","jin zhiwen", "jack", "jay", "king","kevin","kobe",
"lily", "lucy", "mike", "nike", "nail", "open","open cv",
"panda", "pp", "queue", "ray allen", "risk", "tim cook","T-MAC","tony allen",
"x man", "x phone", "yy", "world", "w3c", "zoom","zhu ziqing"};
mList = new ArrayList();
for (int i = 0; i < names.length; i++) {
PhoneContact pc = new PhoneContact(100 + i, names[i], "1861234567"
+ i, names[i].concat("@gmail.com"));
mList.add(pc);
}
}
private void findView() {
mACTV = (AutoCompleteTextView) findViewById(R.id.mACTV);
PhoneAdapter mAdapter = new PhoneAdapter(mList, getApplicationContext());
mACTV.setAdapter(mAdapter);
mACTV.setThreshold(1); //设置输入一个字符 提示,默认为2
mACTV.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
PhoneContact pc = mList.get(position);
mACTV.setText(pc.getName()+" "+pc.getPhone());
}
}
自定义Adapter
package com.example.actv;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.example.actv.entity.PhoneContact;
public class PhoneAdapter extends BaseAdapter implements Filterable {
private ArrayFilter mFilter;
private List mList;
private Context context;
private ArrayList mUnfilteredData;
public PhoneAdapter(List mList, Context context) {
this.mList = mList;
this.context = context;
}
@Override
public int getCount() {
return mList==null ? 0:mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if(convertView==null){
view = View.inflate(context, R.layout.phone_item, null);
holder = new ViewHolder();
holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
holder.tv_phone = (TextView) view.findViewById(R.id.tv_phone);
holder.tv_email = (TextView) view.findViewById(R.id.tv_email);
view.setTag(holder);
}else{
view = convertView;
holder = (ViewHolder) view.getTag();
}
PhoneContact pc = mList.get(position);
holder.tv_name.setText("姓名:"+pc.getName());
holder.tv_phone.setText("电话:"+pc.getPhone());
holder.tv_email.setText("Email:"+pc.getEmail());
return view;
}
static class ViewHolder{
public TextView tv_name;
public TextView tv_phone;
public TextView tv_email;
}
@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 (mUnfilteredData == null) {
mUnfilteredData = new ArrayList(mList);
}
if (prefix == null || prefix.length() == 0) {
ArrayList list = mUnfilteredData;
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();
ArrayList unfilteredValues = mUnfilteredData;
int count = unfilteredValues.size();
ArrayList newValues = new ArrayList(count);
for (int i = 0; i < count; i++) {
PhoneContact pc = unfilteredValues.get(i);
if (pc != null) {
if(pc.getName()!=null && pc.getName().startsWith(prefixString)){
newValues.add(pc);
}else if(pc.getEmail()!=null && pc.getEmail().startsWith(prefixString)){
newValues.add(pc);
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
//noinspection unchecked
mList = (List) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
注意:一定要实现 Filterable 接口,否则无效
MainActivity 布局文件
phone_item.xml