我想在其他视图上放置自定义搜索视图,但不在操作栏上。
与Google地图应用中的搜索相同,左上角有对话框搜索。
输入1个字符以更新arraylist结果。
我不知道自定义和创建方法onQueryTextSubmit,onViewTextChange的SearchView。
如何编码。
谢谢。
答案 0 :(得分:1)
这个概念和ActionBar中的概念相同。您需要将事件与EditText相关联,如下所示:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (myAdapter != null) {
myAdapter.getFilter().filter(charSequence);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
在适配器中,您实现了过滤数据的逻辑。为此,您可以实现Filterable类。尝试这样的事情:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<Object> filteredResult = getFilteredResults(charSequence);
FilterResults results = new FilterResults();
results.values = filteredResult;
results.count = filteredResult.size();
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
listaFiltrada = (ArrayList<Documento>) filterResults.values;
MyAdapter.this.notifyDataSetChanged();
}
private ArrayList<Object> getFilteredResults(CharSequence constraint){
if (constraint.length() == 0){
return listaDocumentos;
}
ArrayList<Object> listResult = new ArrayList<Object>();
for (Object obj : listaTotal){
if (condition){
listResult.add(obj);
}
}
return listResult;
}
};
}
希望它对你有所帮助!! :)
答案 1 :(得分:1)
活动类
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText edittext_search = (EditText) findViewById(R.id.edittext_search);
edittext_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
mAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
适配器类
public class MyAdapter extends ArrayAdapter<String> implements Filterable {
private MyFilter mFilter;
private static List<String> mOriginItmeList;
private static List<String> mItemList;
public MyAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//TODO inflate listview row
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new MyFilter();
}
return mFilter;
}
private class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String search = constraint.toString();
if (TextUtils.isEmpty(search)) {
results.values = mOriginItmeList;
results.count = mOriginItmeList.size();
} else {
ArrayList<String> tempList = new ArrayList<String>();
for (String item : mOriginItmeList) {
if (item==search) {
tempList.add(item);
}
}
results.values = tempList;
results.count = tempList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mItemList = (ArrayList<String>) results.values;
}
}
}