gpt4 book ai didi

android - 显示针对空 SearchView 的建议

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:41 24 4
gpt4 key购买 nike

我的操作栏中有一个 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;
}
}

但是,我希望搜索小部件一打开就弹出建议,而不是像现在这样需要第二次点击。我没有运气让它这样做。

我什至尝试手动查找 SearchViewAutoCompleteTextView 并在 SearchView 时调用 showDropDown()获得焦点(见下文),但也没有显示下拉列表。

mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
int autoCompleteId = mContext.getResources().getIdentifier("android:id/search_src_text", null, null);
if (autoCompleteId == 0) {
// We won't be able to find the view. Give up.
return;
}

AutoCompleteTextView autoCompleteView = (AutoCompleteTextView) mSearchView.findViewById(autoCompleteId);
if (autoCompleteView != null) {
autoCompleteView.showDropDown();
}
}
}

});

如何在打开搜索小部件时使搜索建议下拉?

最佳答案

这是可能的。我正在使用 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();
}

锻炼很痛苦,希望对大家有帮助!

关于android - 显示针对空 SearchView 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25870660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com