布局
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:iconifiedByDefault="false"
android:inputType="textFilter" />
<ListView
android:id="@+id/lv_police"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#ff0000"
android:dividerHeight="1dp"
android:padding="10dp" />```
initData();
public void onSuccess(int statusCode, String content) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, content);
if (content != null) {
PoliceListBean parseObject = JSON.parseObject(
content, PoliceListBean.class);
if (parseObject.getList() != null) {
mData = parseObject.getList();
mBackData = mData;
}
}
initView
mSearchView = (SearchView) findViewById(R.id.searchView);
lv = (ListView) findViewById(R.id.lv_police);
findViewById(R.id.ib_police_back).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("查找");
//mSearchView.setFocusable(false);
lv.setTextFilterEnabled(true);
mAdapter = new MyAdapter();
lv.setAdapter(mAdapter);
自定义Adapter 并添加过滤
``` private class MyAdapter extends BaseAdapter implements Filterable {
private MyFilter mFilter;
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = View.inflate(PoliceActivity.this,
R.layout.police_item, null);
}
TextView name = (TextView) convertView
.findViewById(R.id.tv_police_name);
TextView duiName = (TextView) convertView
.findViewById(R.id.tv_police_duizhang);
TextView number = (TextView) convertView
.findViewById(R.id.tv_police_number);
name.setText(mData.get(position).getJwsmc());
duiName.setText(mData.get(position).getDzxm());
number.setText(mData.get(position).getPddys() + "");
return convertView;
}
@Override
public Filter getFilter() {
if (null == mFilter) {
mFilter = new MyFilter();
}
return mFilter;
}
// 自定义Filter类
class MyFilter extends Filter {
@Override
// 该方法在子线程中执行
// 自定义过滤规则
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<ListBean> newValues = new ArrayList<ListBean>();
String filterString = constraint.toString().trim()
.toLowerCase();
// 如果搜索框内容为空,就恢复原始数据
if (TextUtils.isEmpty(filterString)) {
newValues = mBackData;
} else {
// 过滤出新数据
for (ListBean str : mBackData) {
if (-1 != str.getJwsmc().toLowerCase()
.indexOf(filterString)) {
newValues.add(str);
}
}
}
results.values = newValues;
results.count = newValues.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mData = (List<ListBean>) results.values;
if (results.count > 0) {
mAdapter.notifyDataSetChanged(); // 通知数据发生了改变
} else {
mAdapter.notifyDataSetInvalidated(); // 通知数据失效
}
}
}
}```
需要重写的两个方法,onQueryTextSubmit 是你点击键盘的搜索二字 会触发的方法 直接return false也行。
onQueryTextChange 你每输入一个字符就会触发一次,也会刷新一次适配器:
``` @Override
public boolean onQueryTextSubmit(String query) {
if (mSearchView != null) {
// 得到输入管理对象
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
// 这将让键盘在所有的情况下都被隐藏
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0); // 输入法如果是显示状态,那么就隐藏输入法
}
mSearchView.clearFocus(); // 不获取焦点
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(newText)) {
// 清楚ListView的过滤
mAdapter.getFilter().filter("");
// lv.clearTextFilter();
} else {
// 使用用户输入的内容对ListView的列表项进行过滤
// 主要是这句写上就没黑框了
mAdapter.getFilter().filter(newText);
// lv.setFilterText(newText);
}
return true;
}```
![QQ截图20170626163610.png](http://upload-images.jianshu.io/upload_images/5960267-460f7c89f77476f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
去除黑色背景
不获取焦点无效
实时查询动态刷新