我的操作栏中有一个SearchView
窗口小部件,当用户输入一个或多个字符时,它会显示正确的建议搜索字词。
我想在SearchView
为空时显示(不同的)建议列表,包括在用户输入任何文本之前。
到目前为止,当用户输入文本然后删除该文本时,它正常工作,当用户点击搜索图标打开窗口小部件时,一旦打开,第二次点击搜索窗口小部件。为此,我正在使用OnQueryTextListener
,如此:
class SearchTextEnteredListener implements SearchView.OnQueryTextListener {
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 1) {
// Set normal suggestions
CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext, suggestions);
mSearchView.setSuggestionsAdapter(searchadapter);
} else if (newText.length() == 1) {
// Clear the displayed suggestions
if(searchadapter != null && mSearchView != null){
searchadapter.createAndChangeCursor(null);
}
} else {
// Length = 0, show other suggestions
CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext, otherSuggestions);
mSearchView.setSuggestionsAdapter(searchadapter);
}
return false;
}
}
但是,我希望一旦打开搜索小部件就会弹出建议,而不是像现在这样需要第二次点击。我没有运气这么做。
我甚至尝试手动查找SearchView
的{{1}}并在AutoCompleteTextView
获得焦点时调用showDropDown()
(见下文),但是没有显示下拉。或者
SearchView
如何在打开搜索小部件时创建搜索建议下拉列表?
答案 0 :(得分:3)
有可能。我使用compat v7 SearchView和Cursor Loaders与Activity生命周期协调查询。
// e.g in onCreateView in my Fragment, wire up the SearchView:
MenuItem searchMenuItem = toolbar.getMenu().findItem(R.id.search_menu_item);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
// and setup listeners (implementation is shown later)
searchView.setOnSuggestionListener(this);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
// then raid the SearchView so search begins on 0 chars!
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
autoCompleteTextView.setThreshold(0);
// and set the suggestions adapter so we can use a Loader to do the queries
SuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(getContext(), null, 0);
searchView.setSuggestionsAdapter(suggestionAdapter);
以下是侦听器的实现:
@Override
public boolean onQueryTextChange(String userInput) {
if (TextUtils.isEmpty(userInput.trim())) {
Log.d(TAG, "onQueryTextChange: empty userInput");
// The argument to the loader is null so your query should return all suggestions
getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID, null, this);
return true;
}
userInput = userInput.trim();
Bundle args = new Bundle();
args.putString("USER_INPUT", userInput);
// The argument contains the users input so your query should use a LIKE to find matching suggestions
getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID, args, this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onClose() {
return false;
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
Cursor c = searchView.getSuggestionsAdapter().getCursor();
final String suggestion = c.getString(c.getColumnIndex("some_column_name"));
Log.d(TAG, "onSuggestionClick: position: " + position + " suggestion: " + suggestion);
return true;
}
另外一个细节,我的SuggestionCursorAdapter扩展了CursorAdaptor并覆盖了以下方法以防止: java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteQuery
@Override
public void changeCursor(Cursor newCursor) {
newCursor.close();
}
锻炼是痛苦的,所以希望它可以帮助别人!
答案 1 :(得分:0)
初始化搜索视图
SearchView searchView = findViewById(R.id.viewSearch);
在0个文本上进行searchview
搜索,首先显示最近的项目
AutoCompleteTextView autoCompleteTextView =
searchView.findViewById(androidx.appcompat.R.id.search_src_text);
autoCompleteTextView.setThreshold(0);
在searchView.setOnQueryTextFocusChangeListener
上调用适配器
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Cursor cursor = database.getSearchValue();
Log.d("qqqqq", "onQuery: " + cursor.getCount());
if (cursor.getCount() != 0) {
String[] columns = new String[]{SuggestionSearchDatabase.FIELD_SEARCH};
for (String data : new ArrayList<>(Arrays.asList(columns))) {
Log.i("dataCheck", "onClick: " + data);
}
Log.d("sss", "onClick: " + columns.length);
int[] columnTextId = new int[]{android.R.id.text1};
CursorSearchAdapter searchAdapter = new CursorSearchAdapter(getBaseContext(), R.layout.view_searchview_layout, cursor, columns, columnTextId, 0);
searchView.setSuggestionsAdapter(searchAdapter);
}
}
});