Is there any listener on Android SearchView to notify if SearchView is expanded and ready to take input?

前端 未结 4 1557
攒了一身酷
攒了一身酷 2021-02-13 11:10

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

相关标签:
4条回答
  • 2021-02-13 11:49

    You can set an expand/collapse listener on the MenuItem like this:

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

    This listener was introduced with API level 14, so for backwards compatibility you have to use the v4 support library. If you do then you have to set the expand/collapse listener like this:

    MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {
    
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }
    
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });
    
    0 讨论(0)
  • 2021-02-13 11:50
     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            vwSearch = (SearchView) menu.findItem(R.id.menu_main_search).getActionView();
            vwSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                     //Do something on Submit
                    return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                     //Do something on change
                    return true;
                }
    
    
            });
    
            vwSearch.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    //Do something on collapse Searchview
                    return false;
                }
            });
    
            return true;
        }
    
    0 讨论(0)
  • 2021-02-13 11:52

    the questioner wants to know how to know when the searchview is expanded. just do this with your searchview (assuming its standalone but you can get a reference to it from menu item if needed):

    searchView.setOnSearchClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //search is expanded
                        }
                    });
    

    here is for closed situation:

    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                        @Override
                        public boolean onClose() {
    // searchview closed
                            return false;
                        }
                    });
    
    0 讨论(0)
  • 2021-02-13 12:02

    Try adding this in AndroidManifest.xml along with your activity for example name of your activity is activity1.

    <activity
            android:name="com.example.app1.activity1"
            android:label="@string/title_activity_one"
            android:windowSoftInputMode="adjustPan" >
    </activity>
    

    instead of "adjustPan" you might use "adjustResize"

    0 讨论(0)
提交回复
热议问题