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>

关注公众号,分享干货,讨论技术



原文地址:https://www.cnblogs.com/molashaonian/p/9097668.html