Android 自定义过滤搜索框

http://blog.csdn.net/wyzxk888/article/details/8800407


简单的说就是ListView上面有一个SearchBox,然后searchbox里输入内容后对下面listview进行过滤。

涉及的控件:ListView必须有,EditText用来自定义SearchBox

大概就是这样:

         

先看这个有图片的EditText,实现方法有两个,一是用相对布局RelativeLayout + ImageView + EditText。

二是直接用EditText的一个属性DrawableLeft,简单的UI这个就可以实现了

所以这个Activity的布局就很简单,可以用ListActivity实现:

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <EditText  
  12.         android:id="@+id/searchbox"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:drawableLeft="@drawable/searchbox"  
  17.         android:hint="Search"  
  18.         android:drawablePadding="5dp"  
  19.         android:singleLine="true"  
  20.         android:ems="10" >  
  21.         <requestFocus />  
  22.     </EditText>  
  23.   
  24.       
  25.     <ListView   
  26.         android:id="@android:id/list"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_below="@id/searchbox">  
  30.     </ListView>  
  31.    
  32. </RelativeLayout>  

再说过滤功能:这个感觉不想搜索,就像是简单的过滤,如果涉及到去数据库取数据那个才是搜索了

用到了Filterable接口,Filter类

要让数据有过滤功能,我们需要在继承的BaseAdapter的基础上再实现Filterable接口的getFilter方法,同时在Adapter内部写一个继承Filter的内部类来完成过滤功能:

[java]  view plain copy
  1. private class ListAdapter extends BaseAdapter implements Filterable {  
  2.   
  3.         private List<Person> list;  
  4.           
  5.         private Context context;  
  6.           
  7.         private PersonFilter filter;  
  8.           
  9.         public ListAdapter(List<Person> list, Context context) {  
  10.             this.list = list;  
  11.             this.context = context;  
  12.         }  
  13.   
  14.         @Override  
  15.         public int getCount() {  
  16.             return list.size();  
  17.         }  
  18.   
  19.         @Override  
  20.         public Object getItem(int position) {  
  21.             return list.get(position);  
  22.         }  
  23.   
  24.         @Override  
  25.         public long getItemId(int position) {  
  26.             return position;  
  27.         }  
  28.   
  29.         @Override  
  30.         public View getView(int position, View convertView, ViewGroup parent) {  
  31.             if (convertView == null) {  
  32.                 convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);  
  33.             }  
  34.             Person p = list.get(position);  
  35.             TextView firstname = (TextView)convertView.findViewById(R.id.firstname);  
  36.             TextView lastname = (TextView)convertView.findViewById(R.id.lastname);  
  37.             TextView age = (TextView)convertView.findViewById(R.id.age);  
  38.               
  39.             firstname.setText(p.firstname);  
  40.             lastname.setText(p.lastname);  
  41.             age.setText(p.age + "");  
  42.             return convertView;  
  43.         }  
  44.   
  45.         @Override  
  46.         public Filter getFilter() {  
  47.             if (filter == null) {  
  48.                 filter = new PersonFilter(list);  
  49.             }  
  50.             return filter;  
  51.         }  
  52.           
  53.         private class PersonFilter extends Filter {  
  54.               
  55.             private List<Person> original;  
  56.               
  57.             public PersonFilter(List<Person> list) {  
  58.                 this.original = list;  
  59.             }  
  60.   
  61.             @Override  
  62.             protected FilterResults performFiltering(CharSequence constraint) {  
  63.                 FilterResults results = new FilterResults();  
  64.                 if (constraint == null || constraint.length() == 0) {  
  65.                     results.values = original;  
  66.                     results.count = original.size();  
  67.                 } else {  
  68.                     List<Person> mList = new ArrayList<Person>();  
  69.                     for (Person p: original) {  
  70.                         if (p.firstname.toUpperCase().startsWith(constraint.toString().toUpperCase())  
  71.                             || p.lastname.toUpperCase().startsWith(constraint.toString().toUpperCase())  
  72.                             || new String(p.age + "").toUpperCase().startsWith(constraint.toString().toUpperCase())) {  
  73.                             mList.add(p);  
  74.                         }  
  75.                     }  
  76.                     results.values = mList;  
  77.                     results.count = mList.size();  
  78.                 }  
  79.                 return results;  
  80.             }  
  81.   
  82.             @Override  
  83.             protected void publishResults(CharSequence constraint,  
  84.                     FilterResults results) {  
  85.                 list = (List<Person>)results.values;  
  86.                 notifyDataSetChanged();  
  87.             }  
  88.               
  89.         }  
  90.     }  

Filter类中的两个方法看名字就是知道一个是执行过滤的,一个刷新listview数据展现结果的。其中我采用了前缀匹配,就是用输入的字符串和ListView里的所有Person的各个属性的前缀做比较。也可以用更加复杂的匹配,例如正则表达式。

关键在于EditText里的数据是如何传入的,要写一个TextWater,并且要让EditText注册一下这个监听器:

[java]  view plain copy
  1. private TextWatcher filterTextWatcher = new TextWatcher() {  
  2.   
  3.         @Override  
  4.         public void afterTextChanged(Editable s) {  
  5.   
  6.         }  
  7.   
  8.         @Override  
  9.         public void beforeTextChanged(CharSequence s, int start, int count,  
  10.                 int after) {  
  11.   
  12.         }  
  13.   
  14.         @Override  
  15.         public void onTextChanged(CharSequence s, int start, int before,  
  16.                 int count) {  
  17.             listAdapter.getFilter().filter(s); //这里传入数据就可以了  
  18.         }  
  19.   
  20.     };  

以上关键代码。非关键代码是Person类以及List_Item的布局:

[java]  view plain copy
  1. private class Person {  
  2.         public String firstname;  
  3.         public String lastname;  
  4.         public int age;  
  5.           
  6.         public Person(String firtname, String lastname, int age) {  
  7.             this.firstname = firtname;  
  8.             this.lastname = lastname;  
  9.             this.age = age;  
  10.         }  
  11.     }  

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/firstname"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_margin="10dp"  
  12.         android:text="Firstname" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/lastname"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_margin="10dp"  
  19.         android:text="Lastname" />  
  20.       
  21.     <TextView  
  22.         android:id="@+id/age"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_margin="10dp"  
  26.         android:text="Age" />  
  27.   
  28. </LinearLayout>  



更多 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值