public class SubscribeActivity extends SherlockFragmentActivity implements OnQueryTextListener, OnItemClickListener, OnItemLongClickListener { private SearchView mSearchView; private ListView mListView; private SubscribeIndexAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { //... mAdapter = new SubscribeIndexAdapter(this, mSubcribeList); mSearchView = (SearchView) findViewById(R.id.search_view); mListView = mFooter.getListViewById(R.id.list_view); mListView.setAdapter(mAdapter); mListView.setTextFilterEnabled(true); mListView.setOnItemClickListener(this); mListView.setOnItemLongClickListener(this); setupSearchView(); //... } private void setupSearchView() { mSearchView.setIconifiedByDefault(false); mSearchView.setOnQueryTextListener(this); mSearchView.setSubmitButtonEnabled(false); mSearchView.setQueryHint("搜索"); } public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { mListView.clearTextFilter(); } else { mListView.setFilterText(newText.toString()); } return true; } public boolean onQueryTextSubmit(String query) { return false; } }
public class SubscribeIndexAdapter extends BaseAdapter implements Filterable { private LayoutInflater inflater; private ArrayList<Subscribe> mSubcribeList; private MyFilter mFilter; private ArrayList<Subscribe> mOriginalValues; private final Object mLock = new Object(); static class ViewHolder { TextView textView; ImageView imageView; ImageView imageView2; } public SubscribeIndexAdapter(Context context, ArrayList<Subscribe> subcribeList) { mSubcribeList = subcribeList; inflater = LayoutInflater.from(context); } @Override public int getCount() { return mSubcribeList.size(); } @Override public Object getItem(int position) { return mSubcribeList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //... return convertView; } @Override public Filter getFilter() { if (mFilter == null) { mFilter = new MyFilter(); } return mFilter; } class MyFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { mOriginalValues = new ArrayList<Subscribe>(mSubcribeList); } } if (prefix == null || prefix.length() == 0) { synchronized (mLock) { ArrayList<Subscribe> list = new ArrayList<Subscribe>( mOriginalValues); results.values = list; results.count = list.size(); } } else { String prefixString = prefix.toString().toLowerCase(); final ArrayList<Subscribe> values = mOriginalValues; final int count = values.size(); final ArrayList<Subscribe> newValues = new ArrayList<Subscribe>( count); for (Subscribe value : values) { String title = value.title.toLowerCase(); if (title.indexOf(prefixString) != -1) { newValues.add(value); } } results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { mSubcribeList = (ArrayList<Subscribe>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } }