gpt4 book ai didi

android - Android SearchView 上是否有任何监听器通知 SearchView 是否已展开并准备好接受输入?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:57 32 4
gpt4 key购买 nike

我想在用户未输入任何内容时在 SearchView 中显示一些默认建议。我正在使用矩阵光标手动设置我的自定义建议适配器。我尝试在 onFocusChange 和 onClickListner 中设置适配器,但建议隐藏在键盘后面,显然建议加载早于键盘。我没有找到任何监听器告诉我 searchview 在 View 中完全加载了键盘。帮助我找到正确的解决方案或任何解决方法。我已经尝试过 postDelayed 的解决方法,但体验并不顺畅。

我在 onCreateOptionsMenu 中有这段代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
final MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
this.menu=menu;
basketProductCount = ShoppinglistDataSource.get(getContext()).getProductCountToOrder();
if(basketProductCount>0 && menu!=null){
this.activateBasket();
}

SearchManager manager = (SearchManager) getSystemService(SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchMenuItem.getActionView();

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

int searchButtonId = mSearchView.getContext().getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchButton = (ImageView) mSearchView.findViewById(searchButtonId);
if(searchButton!=null)
searchButton.setImageResource(R.drawable.search);



mSearchView.setSearchableInfo(manager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchProductActivity.class)));
mSearchView.setIconifiedByDefault(true);
mSearchView.setIconified(true);
mSearchView.setQueryHint("Search from 4000+ products");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String s) {
Log.d("SEARCH_VIEW_DEBUG","Inside onQueryTextSubmit for query: "+s);
mSearchView.clearFocus();
mSearchView.setIconified(true);
//return false to let the SearchView perform the default action
return false;
}

@Override
public boolean onQueryTextChange(String query) {
query = query.toLowerCase().trim();
Log.d("Query changed to ",query);
if (query.length() > 1) {
Bundle b = new Bundle();
b.putString("query",query);
if(getLoaderManager().getLoader(1)!=null)
getLoaderManager().restartLoader(1, b, suggestionLoaderCallback);
else
getLoaderManager().initLoader(1, b, suggestionLoaderCallback);
}
return true;
}

});

mSearchView.setOnQueryTextFocusChangeListener(new SearchView.OnFocusChangeListener() {

@Override
public void onFocusChange(View view, boolean queryTextFocused) {
Log.d("DEBUGGIN", "onFocusChange");
if(!queryTextFocused) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mSearchView.onActionViewCollapsed();
mSearchView.clearFocus();
}
} else {
/**This is doing the trick, but not smooth experience**/
/**also suggestions may get behind the keyboard if keyboard takes time to pop up**/
view.postDelayed(new Runnable() {
@Override
public void run() {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mSearchView.onActionViewExpanded();
Log.d("DEBUGGIN", "Inside expand");
if (mSearchView.getQuery().length() == 0) {

Log.d("DEBUGGIN", "getting default suggestions");
sug.clear();
sug.add("Personal Care");
sug.add("Oil");
sug.add("Spices");
sug.add("Breakfast");
sug.add("Snacks");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
String[] columns = new String[]{"_id", "text"};
Object[] temp = new Object[]{0, "default"};
final MatrixCursor cursor = new MatrixCursor(columns);
int i = 0;
for (i = 0; i < sug.size(); i++) {
if (!sug.get(i).equals("")) {
temp[0] = i;
temp[1] = sug.get(i);
cursor.addRow(temp);
}
}
Log.d("Initial categories", sug.toString());
mSearchView.setSuggestionsAdapter(new CustomSuggestionAdapter(ListAll.this, cursor, sug));
mSearchView.getSuggestionsAdapter().notifyDataSetChanged();
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
Log.d("SEARCH_VIEW_DEBUG", "Inside onSuggestionSelect");



return false;
}

@Override
public boolean onSuggestionClick(int i) {
Log.d("SEARCH_VIEW_DEBUG", "Inside onSuggestionClick");
MatrixCursor cursor = (MatrixCursor) mSearchView.getSuggestionsAdapter().getItem(i);
String suggest1 = cursor.getString(cursor
.getColumnIndex("text"));
Log.d("Clicked Item", suggest1);
mSearchView.setQuery(suggest1, true);
return true;
}
});
}
}
}

}
}, 600);
}
}
});
}

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside onMenuItemActionExpand");
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside onMenuItemActionCollapse");
return true;
}
});
} else {
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside MenuItemCompat onMenuItemActionExpand");
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside MenuItemCompat onMenuItemActionCollapse");
return true;
}
});
}

//return true onCreateOptionsMenu
return true;
}

最佳答案

您可以像这样在 MenuItem 上设置展开/折叠监听器:

menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});

此监听器是在 API 级别 14 中引入的,因此为了向后兼容,您必须使用 v4 支持库。如果这样做,则必须像这样设置展开/折叠监听器:

MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});

关于android - Android SearchView 上是否有任何监听器通知 SearchView 是否已展开并准备好接受输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158248/

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