问题描述
我想告诉一些默认建议,搜索查看,当用户未输入任何东西。我手动设置我的自定义建议适配器使用矩阵光标。我尝试设置适配器onFocusChange和onClickListner但建议得到隐藏在键盘背后,显然建议是早于键盘加载。我没有找到任何监听器,它告诉我,搜索查看是完全加载键盘在视图中。帮我找到合适的解决方案或解决方法。我已经尝试了解决方法postDelayed但它不是流畅的使用体验。
我里面onCreateOptionsMenu这个code
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
menu.clear();
最后MenuInflater充气= this.getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu,菜单);
this.menu =菜单;
basketProductCount = ShoppinglistDataSource.get(的getContext())getProductCountToOrder()。
如果(basketProductCount大于0&安培;&安培;!菜单=空){
this.activateBasket();
}
SearchManager经理=(SearchManager)getSystemService(SEARCH_SERVICE);
菜单项searchMenuItem = menu.findItem(R.id.action_search);
mSearchView =(搜索查看)searchMenuItem.getActionView();
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
INT searchButtonId = mSearchView.getContext()getResources()则getIdentifier(机器人:ID / search_button,NULL,NULL);。
ImageView的searchButton =(ImageView的)mSearchView.findViewById(searchButtonId);
如果(searchButton!= NULL)
searchButton.setImageResource(R.drawable.search);
mSearchView.setSearchableInfo(manager.getSearchableInfo(新单元名(getApplicationContext(),SearchProductActivity.class)));
mSearchView.setIconifiedByDefault(真正的);
mSearchView.setIconified(真正的);
mSearchView.setQueryHint(从4000+产品搜索);
mSearchView.setOnQueryTextListener(新SearchView.OnQueryTextListener(){
@覆盖
公共布尔onQueryTextSubmit(String s)将{
Log.d(SEARCH_VIEW_DEBUG,内部onQueryTextSubmit查询:+ S);
mSearchView.clearFocus();
mSearchView.setIconified(真正的);
//返回false让搜索查看执行的默认操作
返回false;
}
@覆盖
公共布尔onQueryTextChange(查询字符串){
查询= query.toLowerCase()修剪()。
Log.d(查询改为,查询);
如果(query.length()→1){
叠B =新包();
b.putString(查询,查询);
如果(getLoaderManager()。getLoader(1)!= NULL)
。getLoaderManager()restartLoader(1,B,suggestionLoaderCallback);
其他
。getLoaderManager()initLoader(1,B,suggestionLoaderCallback);
}
返回true;
}
});
mSearchView.setOnQueryTextFocusChangeListener(新SearchView.OnFocusChangeListener(){
@覆盖
公共无效onFocusChange(查看视图,布尔queryTextFocused){
Log.d(DEBUGGIN,onFocusChange);
如果(!queryTextFocused){
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
mSearchView.onActionViewCollapsed();
mSearchView.clearFocus();
}
} 其他 {
/ **这是做的伎俩,但不流畅的体验** /
/ **还建议,可以获取键盘的背后,如果键盘需要时间来弹出** /
view.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
mSearchView.onActionViewExpanded();
Log.d(DEBUGGIN,内部扩展);
如果(mSearchView.getQuery()。长度()== 0){
Log.d(DEBUGGIN,得到默认的建议);
sug.clear();
sug.add(个人护理);
sug.add(油);
sug.add(香料);
sug.add(早餐);
sug.add(零食);
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
的String []列=新的String [] {_ ID,文本};
[对象]临时=新的对象[] {0,默认};
最后MatrixCursor光标=新MatrixCursor(列);
INT I = 0;
对于(i = 0; I< sug.size();我++){
如果(!sug.get(ⅰ).equals()){
温度[0] = I;
温度[1] = sug.get(ⅰ);
cursor.addRow(临时);
}
}
Log.d(头文字类,sug.toString());
mSearchView.setSuggestionsAdapter(新CustomSuggestionAdapter(ListAll.this,光标,SUG));
mSearchView.getSuggestionsAdapter()notifyDataSetChanged()。
mSearchView.setOnSuggestionListener(新SearchView.OnSuggestionListener(){
@覆盖
公共布尔onSuggestionSelect(int i)以{
Log.d(SEARCH_VIEW_DEBUG,内部onSuggestionSelect);
返回false;
}
@覆盖
公共布尔onSuggestionClick(int i)以{
Log.d(SEARCH_VIEW_DEBUG,内部onSuggestionClick);
MatrixCursor光标=(MatrixCursor)mSearchView.getSuggestionsAdapter()的getItem(i)中。;
字符串suggest1 = cursor.getString(光标
.getColumnIndex(文本));
Log.d(单击项目,suggest1);
mSearchView.setQuery(suggest1,真正的);
返回true;
}
});
}
}
}
}
},600);
}
}
});
}
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
searchMenuItem.setOnActionExpandListener(新MenuItem.OnActionExpandListener(){
@覆盖
公共布尔onMenuItemActionExpand(菜单项项){
/ **这个不会被调用** /
Log.d(DEBUGGIN,内部onMenuItemActionExpand);
返回true;
}
@覆盖
公共布尔onMenuItemActionCollapse(菜单项项){
/ **这个不会被调用** /
Log.d(DEBUGGIN,内部onMenuItemActionCollapse);
返回true;
}
});
} 其他 {
MenuItemCompat.setOnActionExpandListener(searchMenuItem,新MenuItemCompat.OnActionExpandListener(){
@覆盖
公共布尔onMenuItemActionExpand(菜单项项){
/ **这个不会被调用** /
Log.d(DEBUGGIN,内部MenuItemCompat onMenuItemActionExpand);
返回true;
}
@覆盖
公共布尔onMenuItemActionCollapse(菜单项项){
/ **这个不会被调用** /
Log.d(DEBUGGIN,内部MenuItemCompat onMenuItemActionCollapse);
返回true;
}
});
}
//返回true onCreateOptionsMenu
返回true;
}
我的情况是类似的,但略有不同,因为我用SearchViewCompat,但你可以轻松适应我对你的需求的解决方案。基本上,你需要包装在包装其子类的视图(实际上的LinearLayout)和手表的大小来改变你的搜索视图。该包装,然后设置为而ActionView的菜单项。
查看搜索查看= SearchViewCompat.newSearchView(getActivity());
的LinearLayout wrapperView =新的LinearLayout(getActivity()){
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
如果(oldw大于0&安培;&安培;并且R w oldw){
新的处理程序()。postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
//这里处理您的'扩展'事件
}
},10);
}
super.onSizeChanged(W,H,oldw,oldh);
}
};
searchView.setLayoutParams(新ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
wrapperView.addView(搜索查看);
menuItem.setActionView(wrapperView);
I want to show some default suggestions in SearchView when User hasn't typed anything. I am setting my custom suggestion adapter manually using matrix cursor. I tried setting adapter in onFocusChange and onClickListner but suggestions get hidden behind a keyboard, apparently suggestions are loading earlier than keyboard.I did not find any listener which tells me that searchview is completely loaded with keyboard in the view.Help me in finding the right solution or any workaround. I have tried a workaround postDelayed but it's not smooth experience.
I have this code inside 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;
}
My situation is similar but slightly different, since I use SearchViewCompat, but you can easily adapt my solution for your needs. Basically, you need to wrap your search view in a wrapper which subclasses View (actually LinearLayout) and watches for the size to change. The wrapper is then set as the actionView for the menuItem.
View searchView = SearchViewCompat.newSearchView(getActivity());
LinearLayout wrapperView = new LinearLayout(getActivity()) {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (oldw > 0 && w > oldw) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// handle your 'expanded' event here
}
}, 10);
}
super.onSizeChanged(w, h, oldw, oldh);
}
};
searchView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
wrapperView.addView(searchView);
menuItem.setActionView(wrapperView);
这篇关于是否有关于Android的搜索查看任何监听器通知,如果搜索查看扩大,并准备接受输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!