use of com.lapism.searchview.SearchView in project SearchView by lapism.
the class BaseActivity method setSearchView.
// it can be in OnCreate
protected void setSearchView() {
mHistoryDatabase = new SearchHistoryTable(this);
mSearchView = (SearchView) findViewById(R.id.searchView);
if (mSearchView != null) {
mSearchView.setHint(R.string.search);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
getData(query, 0);
mSearchView.close(false);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
mSearchView.setOnOpenCloseListener(new SearchView.OnOpenCloseListener() {
@Override
public boolean onOpen() {
if (mFab != null) {
mFab.hide();
}
return true;
}
@Override
public boolean onClose() {
if (mFab != null && !mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mFab.show();
}
return true;
}
});
mSearchView.setVoiceText("Set permission on Android 6.0+ !");
mSearchView.setOnVoiceClickListener(new SearchView.OnVoiceClickListener() {
@Override
public void onVoiceClick() {
// permission
}
});
List<SearchItem> suggestionsList = new ArrayList<>();
suggestionsList.add(new SearchItem("search1"));
suggestionsList.add(new SearchItem("search2"));
suggestionsList.add(new SearchItem("search3"));
SearchAdapter searchAdapter = new SearchAdapter(this, suggestionsList);
searchAdapter.addOnItemClickListener(new SearchAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
TextView textView = (TextView) view.findViewById(R.id.textView_item_text);
String query = textView.getText().toString();
getData(query, position);
mSearchView.close(false);
}
});
mSearchView.setAdapter(searchAdapter);
/*suggestionsList.add(new SearchItem("search12"));
suggestionsList.add(new SearchItem("search22"));
suggestionsList.add(new SearchItem("search32"));
searchAdapter.notifyDataSetChanged();*/
/*
List<SearchFilter> filter = new ArrayList<>();
filter.add(new SearchFilter("Filter1", true));
filter.add(new SearchFilter("Filter2", true));
mSearchView.setFilters(filter);
//use mSearchView.getFiltersStates() to consider filter when performing search
*/
}
}
Aggregations