http://blog.csdn.net/jiahui524
相信大家一定在见过手机通讯录的一个情景就是使用在选人的时候输入文本框里的数据就能自动筛选。今天也受一网友邀请,所以还是整理了一下这块的知识点,现在与大家共享一下,有什么疑惑的可以与我交流。实现的效果如下图。
其实实现这样的效果相信大家一定对另外一个控件不陌生那就AutoCompleteTextview,看一下这个控件的源代码也许就能找到你需要的答案。这里的核心就是一个Filterable。至于Filterable的介绍和做什么用的大家就可以自行上网查找相关知识,其中有两个重要的方法
方法名 | 作用 |
protected FilterResults performFiltering(CharSequence prefix) | 在这个方法里执行过滤方法 |
protected void publishResults(CharSequence constraint, FilterResults results) | 在这个方法里发布筛选过后得到的数据同时更新Adapter更新 |
理解这点知识那么就看核心代码吧,这里就是重写BaseAdapter然后实现Filterable
- public class UserAdapter extends BaseAdapter implements Filterable {
- private MyFilter myFilter;
- private List<UserInfo> userInfos;
- private Context context;
- private ArrayList<UserInfo> mOriginalValues;
- private final Object mLock = new Object();
- public UserAdapter(Context context, List<UserInfo> userInfos) {
- this.context = context;
- this.userInfos = userInfos;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return userInfos.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return userInfos.get(arg0);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- ViewHolder holder;
- if (view == null) {
- view = LayoutInflater.from(context).inflate(R.layout.list_item,
- null);
- holder = new ViewHolder();
- holder.tv_nick = (TextView) view.findViewById(R.id.tv_nick);
- holder.tv_mobile = (TextView) view.findViewById(R.id.tv_mobile);
- view.setTag(holder);
- } else {
- holder = (ViewHolder) view.getTag();
- }
- holder.tv_nick.setText(userInfos.get(position).getUsername());
- holder.tv_mobile.setText(userInfos.get(position).getPhonenum());
- return view;
- }
- static class ViewHolder {
- TextView tv_nick;
- TextView tv_mobile;
- }
- @Override
- public Filter getFilter() {
- if (myFilter == null) {
- myFilter = new MyFilter();
- }
- return myFilter;
- }
- class MyFilter extends Filter {
- @Override
- protected FilterResults performFiltering(CharSequence prefix) {
- // 持有过滤操作完成之后的数据。该数据包括过滤操作之后的数据的值以及数量。 count:数量 values包含过滤操作之后的数据的值
- FilterResults results = new FilterResults();
- if (mOriginalValues == null) {
- synchronized (mLock) {
- // 将list的用户 集合转换给这个原始数据的ArrayList
- mOriginalValues = new ArrayList<UserInfo>(userInfos);
- }
- }
- if (prefix == null || prefix.length() == 0) {
- synchronized (mLock) {
- ArrayList<UserInfo> list = new ArrayList<UserInfo>(
- mOriginalValues);
- results.values = list;
- results.count = list.size();
- }
- } else {
- // 做正式的筛选
- String prefixString = prefix.toString().toLowerCase();
- // 声明一个临时的集合对象 将原始数据赋给这个临时变量
- final ArrayList<UserInfo> values = mOriginalValues;
- final int count = values.size();
- // 新的集合对象
- final ArrayList<UserInfo> newValues = new ArrayList<UserInfo>(
- count);
- for (int i = 0; i < count; i++) {
- // 如果姓名的前缀相符或者电话相符就添加到新的集合
- final UserInfo value = (UserInfo) values.get(i);
- Log.i("coder", "PinyinUtils.getAlpha(value.getUsername())"
- + PinyinUtils.getAlpha(value.getUsername()));
- if (PinyinUtils.getAlpha(value.getUsername()).startsWith(
- prefixString)
- || value.getPhonenum().startsWith(prefixString)||value.getUsername().startsWith(prefixString)) {
- newValues.add(value);
- }
- }
- // 然后将这个新的集合数据赋给FilterResults对象
- results.values = newValues;
- results.count = newValues.size();
- }
- return results;
- }
- @Override
- protected void publishResults(CharSequence constraint,
- FilterResults results) {
- // 重新将与适配器相关联的List重赋值一下
- userInfos = (List<UserInfo>) results.values;
- if (results.count > 0) {
- notifyDataSetChanged();
- } else {
- notifyDataSetInvalidated();
- }
- }
- }
- }
public class UserAdapter extends BaseAdapter implements Filterable {
private MyFilter myFilter;
private List<UserInfo> userInfos;
private Context context;
private ArrayList<UserInfo> mOriginalValues;
private final Object mLock = new Object();
public UserAdapter(Context context, List<UserInfo> userInfos) {
this.context = context;
this.userInfos = userInfos;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userInfos.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return userInfos.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item,
null);
holder = new ViewHolder();
holder.tv_nick = (TextView) view.findViewById(R.id.tv_nick);
holder.tv_mobile = (TextView) view.findViewById(R.id.tv_mobile);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tv_nick.setText(userInfos.get(position).getUsername());
holder.tv_mobile.setText(userInfos.get(position).getPhonenum());
return view;
}
static class ViewHolder {
TextView tv_nick;
TextView tv_mobile;
}
@Override
public Filter getFilter() {
if (myFilter == null) {
myFilter = new MyFilter();
}
return myFilter;
}
class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
// 持有过滤操作完成之后的数据。该数据包括过滤操作之后的数据的值以及数量。 count:数量 values包含过滤操作之后的数据的值
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
// 将list的用户 集合转换给这个原始数据的ArrayList
mOriginalValues = new ArrayList<UserInfo>(userInfos);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (mLock) {
ArrayList<UserInfo> list = new ArrayList<UserInfo>(
mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
// 做正式的筛选
String prefixString = prefix.toString().toLowerCase();
// 声明一个临时的集合对象 将原始数据赋给这个临时变量
final ArrayList<UserInfo> values = mOriginalValues;
final int count = values.size();
// 新的集合对象
final ArrayList<UserInfo> newValues = new ArrayList<UserInfo>(
count);
for (int i = 0; i < count; i++) {
// 如果姓名的前缀相符或者电话相符就添加到新的集合
final UserInfo value = (UserInfo) values.get(i);
Log.i("coder", "PinyinUtils.getAlpha(value.getUsername())"
+ PinyinUtils.getAlpha(value.getUsername()));
if (PinyinUtils.getAlpha(value.getUsername()).startsWith(
prefixString)
|| value.getPhonenum().startsWith(prefixString)||value.getUsername().startsWith(prefixString)) {
newValues.add(value);
}
}
// 然后将这个新的集合数据赋给FilterResults对象
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// 重新将与适配器相关联的List重赋值一下
userInfos = (List<UserInfo>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
具体有看不懂的代码可以看注释或者加我QQ
最后再看一下是怎么用的吧
- et_filter.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- adapter.getFilter().filter(et_filter.getText().toString());
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- // TODO Auto-generated method stub
- }
- @Override
- public void afterTextChanged(Editable s) {
- // TODO Auto-generated method stub
- }
- });