Android SearchView结合Filter实现列表检索功能使用总结
下面贴出主要代码:
<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实现列表检索功能使用总结的更多相关文章
- android 开发 实现RecyclerView的列表单选功能
实现思维: 1.首先在一行的xml布局中添加一个选中效果的icon图片,未选中的情况下INVISIBLE或者GONE 都可以,推荐使用INVISIBLE它会占用布局位置但是不显示,这样可以避免布局中其 ...
- 百度地图 Android SDK - 检索功能使用的简单演示样例
百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...
- Android端IM应用中的@人功能实现:仿微博、QQ、微信,零入侵、高可扩展
本文由“猫爸iYao”原创分享,感谢作者. 1.引言 最近有个需求:评论@人(没错,就是IM聊天或者微博APP里的@人功能),就像下图这样: ▲ 微信群聊界面里的@人功能 ▲ QQ群聊界面里 ...
- 如何获得Android手机的软件安装列表
Android的PackageManager类用于检索目前安装在设备上的应用软件包的信息.你可以通过调用getpackagemanager()得到PackageManager类的一个实例.对查询和操作 ...
- PHP多条件分类列表筛选功能开发实例
PHP多条件分类列表筛选功能开发实例,前后台一起实现 后台对接可以拼接sql语句,PHP通过表单值隐藏值筛选,常用又实用! 表单筛选核心函数 function Filter(a, b) { var $ ...
- 【转】一张图解析FastAdmin中的表格列表的功能
一张图解析FastAdmin中的表格列表的功能 功能描述请根据图片上的数字索引查看对应功能说明. 1.时间筛选器如果想在搜索栏使用时间区间进行搜索,则可以在JS中修改修改字段属性,如 {field: ...
- Android 使用ListView的A-Z字母排序功能实现联系人模块
在上一篇文章其中,主要学习了ListView的A-Z字母排序功能以及依据输入框的输入值改变来过滤搜索结果,假设输入框里面的值为空.更新为原来的列表,否则为过滤数据列表,包含汉字转成拼音的功能.假设你还 ...
- 百度地图API 批量添加 带检索功能的信息窗口
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
随机推荐
- mybatis 枚举类型使用
一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...
- vue.js学习之 如何在手机上查看vue-cli构建的项目
vue.js学习之 如何在手机上查看vue-cli构建的项目 一:找到config文件夹下的index.js文件,打开后,将host的值改为你本地的ip,保存后重启项目 二:输入ip和端口号打开项目 ...
- css重修之书(一):如何用css制作比1px更细的边框
如何用css制作比1px更细的边框 在项目的开发过程中,我们常常会使用到border:1px solid xxx,来对元素添加边框: 可是1px的border看起来还是粗了一些粗,不美观,那么有什么方 ...
- 设置启动窗体Program.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- 初入py
1.下载工具sublime 我的网盘下载地址:https://pan.baidu.com/s/18-U1ZSg_zHoSAqUuvXj_PQ 直接解压即可 2.配置py27 在新建的文件里面编辑并保存 ...
- zk分布锁的java实现
只做记录,直接上代码 父类: package com.ylcloud.common.lock; import com.alibaba.fastjson.JSON; import org.I0Itec. ...
- poj3074-Sodoku
解数独. 分析 考虑如何把数独解合法的条件转化为经典的01精确覆盖: 每个格子只能填一个数,1-9 每一列刚好填了1-9 每一行刚好填了1-9 每个九宫格刚好填了1-9 也就是说,每个格子,列,行,九 ...
- P3065 [USACO12DEC]第一!First!
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- Python列表去重的三种方法
1. 列表去重 li = [] for item in my_list: if item not in li: li.append(item) 2.集合去重 list(set(my_list)) 3. ...
- Windows系统Unity3D中的快捷键
Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl N New Scene 新建场景 Ctrl O Open Scene 打开场景 Ctrl S Sav ...