我尝试将最近的搜索添加到工具栏上的SearchView:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (s!= null )
{
if (s.isEmpty()){
loadRecentList();
}
else{
search(s);
}
}
return true;
}
});
搜索工作正常,但我无法查看最近的列表结果。
我发现了Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
但修改ActionBar库不是一个好的解决方案。
有没有人有其他解决方案? 感谢。
编辑:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main_activity, menu);
searchItem = menu.findItem(R.id.btn_earch);
initSearchView();
setSearchViewListener();
return super.onCreateOptionsMenu(menu);
}
protected void initSearchView() {
final String CURSOR_RESOURCE_ID = "mCursorDrawableRes";
final int THRESHOLD = 1;
mSearchView = (SearchView) searchItem.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setMeasureWithLargestChildEnabled(true);
autoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(R.id.search_src_text);
autoCompleteTextView.setThreshold(THRESHOLD);
}
protected void setSearchViewListener() {
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (s!= null )
{
if (s.isEmpty()){
loadRecentList();
}
else{
search(s);
}
}
return true;
}
});
btn_search项目:
<item
android:id="@+id/btn_search"
android:icon="@drawable/ic_search_small"
android:title="@string/tooltip_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
/>
答案 0 :(得分:4)
我用这样的东西解决它
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("MyApp", searchView.getQuery().toString());
consultarListaParadas(searchView.getQuery().toString());
getActivity().runOnUiThread(new Runnable() {
public void run() {
adaptadorListaParadasBus.setListaParadas(listaParadas);
adaptadorListaParadasBus.notifyDataSetChanged();
}
});
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.equals("")){
this.onQueryTextSubmit("");
}
return true;
}
});
唯一的问题是,如果没有按下搜索按钮,它就会抛出。
答案 1 :(得分:0)
@Gali,
请查看我的代码:
1)我创建了ActionBar Activity:
public class CarsActivity extends ActionBarActivity implements ExpandableListView.OnGroupClickListener
{
//my class fields:
private MenuItem _menu_item;
private SearchView _searchView;
// .......
//here is my OnCreateOptionMenu method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_cars_activity, menu);
_menu_item = menu.findItem(R.id.action_search);
_searchView = (SearchView) MenuItemCompat.getActionView(_menu_item);
_searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v)
{
//action when search is displayed on actinbar
}
@Override
public void onViewDetachedFromWindow(View v)
{
//action when search is collaped on actinbar
}
});
_searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s)
{
return true;
}
@Override
public boolean onQueryTextChange(String s)
{
//listener for entering text in searchView
//you can implement it your way
return false;
}
});
return true;
}
}
希望它会对你有所帮助。
答案 2 :(得分:0)
queryTextListener不会触发空字符串事件。解决方案是在SearchView上找到下划线的EditText,并将TextWatcher附加到该文件。
查找EditText视图的递归方法
private EditText getEditText(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
View editText = getEditText(context, child);
if (editText instanceof EditText) {
return (EditText) editText;
}
}
} else if (v instanceof EditText) {
Log.d(LogUtil.RANDOM_TAG, "found edit text");
return (EditText) v;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
然后当你有SearchView
EditText et = getEditText(getActivity(), searchView);
if (et != null) {
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(TextUtils.isEmpty(s)){
Log.d(LogUtil.RANDOM_TAG, "empty");
//do stuff
}
else{
Log.d(LogUtil.RANDOM_TAG, "not empty");
}
}
});
}