gpt4 book ai didi

android - 搜索 View :Triggering onQueryTextChange on Collapsing of the searchView

转载 作者:行者123 更新时间:2023-11-29 19:05:11 26 4
gpt4 key购买 nike

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
search(newText);

return true;
}
});

我不想在 searchview 时执行 search(newText)折叠。

我试过了

if(searchView.isShown) search(newText);

if(!searchView.isIconified) search(newText);

但是当我关闭 searchview , searchView.isShownsearchView.isIconified值将在触发后更新 onQueryTextChange .

我的 onCreateOptionMenu 代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contacts, menu);

addContactMenuItem = menu.findItem(R.id.action_add_contact);
searchViewMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchViewMenuItem);
MenuItemCompat.setOnActionExpandListener(searchViewMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
addContactMenuItem.setVisible(false);
if (contactListAdapter != null)
contactListAdapter.getFilter().filter(searchView.getQuery());
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addContactMenuItem.setVisible(true);
contactListAdapter.performGroupBasedFiltering(groupSpinner.getAdapter().getItemId(groupSpinner.getSelectedItemPosition()));
return true;
}
});

return true;
}

请帮帮我!提前致谢。

最佳答案

试试这个用法 setOnActionExpandListener

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);

MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);

MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// perform your action here
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// perform your action here
return true;
}
});




return true;

}

编辑

as per your comment

我需要 search(newText) 不在折叠时执行。

than set this searchView.setOnQueryTextListener(null); when you don't need

关于android - 搜索 View :Triggering onQueryTextChange on Collapsing of the searchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47546312/

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