下面贴出主要代码:

<span style="font-size:14px;">
public class MainActivity extends FragmentActivity implements OnClickListener{
public static SearchView mSearchView = null;
 public static ListView lv = null;
     private LazyAdapter adapter = null;  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
        lv = (ListView) this.findViewById(R.id.lv);
        adapter = new LazyAdapter(this, list);
lv.setAdapter(adapter);</span><span style="font-size:14px;">  mSearchView = (SearchView) findViewById(R.id.srv);
mSearchView.setIconifiedByDefault(false);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setFocusable(true);
mSearchView.setFocusableInTouchMode(true);
/*
mSearchView.requestFocus();
mSearchView.clearFocus();
mSearchView.onActionViewExpanded();
*/
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
ListAdapter adapter = lv.getAdapter();
if(adapter instanceof Filterable){
Filter filter = ((Filterable)adapter).getFilter();
if(newText==null || newText.length()==0){
filter.filter(null);
}else{
filter.filter(newText);
}
}
return true;
}
});
} @Override
protected void onResume() {
mSearchView.setFocusable(true);
mSearchView.setFocusableInTouchMode(true);
// mSearchView.requestFocus(); //获取焦点
super.onResume();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
} </span>
<span style="font-size:14px;">
public class LazyAdapter extends BaseAdapter implements Filterable{
private Activity activity;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private List<Map<String, String>> list = null;
private List<Map<String, String>> values = null;
private MyFilter mFilter;
private Object mLock = new Object(); public LazyAdapter(Activity a, List<Map<String, String>> list) {
this.activity = a;
this.list = list;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.item_layout, null);
}
TextView text = BaseViewHolder.get(vi, R.id.title);
TextView fu_text = BaseViewHolder.get(vi, R.id.fu_title);
ImageView image=BaseViewHolder.get(vi, R.id.iv);
text.setText(list.get(position).get("title"));
fu_text.setText(list.get(position).get("fu_title"));
imageLoader.DisplayImage(list.get(position).get("src"), image);
return vi;
} @Override
public Filter getFilter() {
if(mFilter==null){
mFilter = new MyFilter();
}
return mFilter;
}
class MyFilter extends Filter{
@SuppressLint("DefaultLocale")
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if(values==null){
synchronized (mLock) {
values = new ArrayList<Map<String, String>>(list);
}
}
if(prefix==null || prefix.length()==0){
synchronized (mLock) {
List<Map<String, String>> list1 = new ArrayList<Map<String, String>>(values);
results.values = list1;
results.count = list1.size();
}
}else{
String prefixString = prefix.toString().toLowerCase();
List<Map<String, String>> values1 = values;
int count = values1.size();
List<Map<String, String>> newValues = new ArrayList<Map<String, String>>(count);
for(Map<String, String> value : values1){
String title = value.get("title").toLowerCase();
if(title.indexOf(prefixString) != -1){
newValues.add(value);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
list = (List<Map<String,String>>) results.values;
XinwenFragment.list = list;
if(results.count>0){
notifyDataSetChanged();
}else{
notifyDataSetInvalidated();
}
}
}
}
 </span>

Android SearchView结合Filter实现列表检索功能使用总结的更多相关文章

  1. android 开发 实现RecyclerView的列表单选功能

    实现思维: 1.首先在一行的xml布局中添加一个选中效果的icon图片,未选中的情况下INVISIBLE或者GONE 都可以,推荐使用INVISIBLE它会占用布局位置但是不显示,这样可以避免布局中其 ...

  2. 百度地图 Android SDK - 检索功能使用的简单演示样例

    百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...

  3. Android端IM应用中的@人功能实现:仿微博、QQ、微信,零入侵、高可扩展

    本文由“猫爸iYao”原创分享,感谢作者. 1.引言 最近有个需求:评论@人(没错,就是IM聊天或者微博APP里的@人功能),就像下图这样:   ▲ 微信群聊界面里的@人功能    ▲ QQ群聊界面里 ...

  4. 如何获得Android手机的软件安装列表

    Android的PackageManager类用于检索目前安装在设备上的应用软件包的信息.你可以通过调用getpackagemanager()得到PackageManager类的一个实例.对查询和操作 ...

  5. PHP多条件分类列表筛选功能开发实例

    PHP多条件分类列表筛选功能开发实例,前后台一起实现 后台对接可以拼接sql语句,PHP通过表单值隐藏值筛选,常用又实用! 表单筛选核心函数 function Filter(a, b) { var $ ...

  6. 【转】一张图解析FastAdmin中的表格列表的功能

     一张图解析FastAdmin中的表格列表的功能 功能描述请根据图片上的数字索引查看对应功能说明. 1.时间筛选器如果想在搜索栏使用时间区间进行搜索,则可以在JS中修改修改字段属性,如 {field: ...

  7. Android 使用ListView的A-Z字母排序功能实现联系人模块

    在上一篇文章其中,主要学习了ListView的A-Z字母排序功能以及依据输入框的输入值改变来过滤搜索结果,假设输入框里面的值为空.更新为原来的列表,否则为过滤数据列表,包含汉字转成拼音的功能.假设你还 ...

  8. 百度地图API 批量添加 带检索功能的信息窗口

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

随机推荐

  1. IntelliJ IDEA for MAC 注释模板、快捷键生成注释

    增加注释 在IntelliJ IDEA中为JAVA代码增加注释,首先需要配置注释模板,而后使用模板快捷键生成注释, 下面按照[配置模板].[模板使用]两部分进行介绍 ----------------- ...

  2. USACO 1.1.3 Friday the Thirteenth 黑色星期五

    Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...

  3. c# load xml 中文报错

    <?xml version="1.0" encoding="GB2312"?>

  4. PAT 甲级 1048 Find Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...

  5. seaj和requirejs模块化的简单案例

    如今,webpack.gulp等构件工具流行,有人说seajs.requirejs等纯前端的模块化工具已经被淘汰了,我不这么认为,毕竟纯前端领域想要实现模块化就官方来讲,还是有一段路要走的.也因此纯前 ...

  6. python爬虫--打开爬取页面

    def requests_view(response): import webbrowser requests_url = response.url base_url = '<head>& ...

  7. C跟C++

    工作以来几乎就只写C,下周开始的新工作可能就要开始写C++啦~ C: C++: 1.template 2.多态.重载.继承 3.虚函数 C++内存布局 看两张耗子的图片就清楚了.其中虚函数表一般放在数 ...

  8. 【重读MSDN之ADO.NET】ADO.NET连接

    连接到ADO.NET中的数据源 在 ADO.NET 中,通过在连接字符串中提供必要的身份验证信息,使用 Connection 对象连接到特定的数据源.使用的 Connection 对象取决于数据源的类 ...

  9. 如何设计好的RESTful API之安全性

    保证RESTful API的安全性,主要包括三大方面: a) 对客户端做身份认证 b) 对敏感的数据做加密,并且防止篡改 c) 身份认证之后的授权 1.对客户端做身份认证,有几种常见的做法: 1)在请 ...

  10. RT-thread内核之消息队列

    一.消息队列控制块:在include/rtdef.h中 #ifdef RT_USING_MESSAGEQUEUE /** * message queue structure */ struct rt_ ...