下面贴出主要代码:

<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>

最新文章

  1. AMap行政区查询服务
  2. WPF ComboBox Binding
  3. SQL Server里因丢失索引造成的死锁
  4. Java 字节码
  5. linux 监控系统缓存和cpu
  6. DLL技术应用04 - 零基础入门学习Delphi47
  7. 3.Swift翻译教程系列——Swift基础知识
  8. Servlet程序开发-- Servlet生命周期
  9. 自创最精简的python装饰器
  10. printk的用法
  11. 下一站 java
  12. python查看及修改当前的工作路径
  13. excel2007自定义菜单项学习
  14. C#版ObjectId
  15. 嵌入式&#160;探讨父子线程、进程终止顺序不同产生的结果_skdkjxy_新浪博客
  16. leetcode刷题吧
  17. Python continue 语句
  18. HTML 5+CSS 3网页设计经典范例 (李俊民,黄盛奎) 随书光盘​
  19. AngularJS 四 服务
  20. [OpenJudge8786][暴力DP]方格取数

热门文章

  1. CodeForces 908C. New Year and Curling 解题报告 Java
  2. Java静态方法,静态变量,初始化顺序
  3. VBA基础之Excel VBA 表格的操作(一)
  4. javascript的原始类型(primitive type)之间的关系。
  5. Thunder团队第六周 - Scrum会7
  6. 创建、编译、执行 java程序
  7. TCP标志位简析
  8. DELPHI Showmodal 模式窗体
  9. JavaScript 垃圾回收总结
  10. BZOJ 1103 大都市(dfs序+树状数组)