SearchView.OnCloseListener does not get invoked

后端 未结 9 616
慢半拍i
慢半拍i 2020-12-03 22:23

Im using an action bar and adding a searchView to it. I have implemented the searchView.onCLoseListener but this does not seem to be getting invoked. Any suggestions ?

相关标签:
9条回答
  • 2020-12-03 22:44

    If you have set the

    searchView.setIconifiedByDefault(false)
    

    then, your searchView never closes as it is always in expanded mode. This is the reason why your onClose() is not being invoked.

    0 讨论(0)
  • 2020-12-03 22:45

    I'm also find this problem. the solution is very simple! Just store your SearchView view in private var. Then use

    • isShown method to check if view was expanded,
    • searchview.addOnLayoutChange to determine searchview start using isShown after if block do your actions,
    • override onBackPressed with if block(searchView !=null && searchView.isShown() then do your actions when user click back button,
    • and override home(back) button listener in onOptionsItemSelected with isShown block. Note: in menu item xml file remove app:showAsAction collapseActionView flag.
    0 讨论(0)
  • 2020-12-03 22:46

    Ok. i got the mistake. We cant add a searchCommand and do

    setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW)
    

    Doing this would remove the searchText if any and hence we cant do a onClose().

    0 讨论(0)
  • 2020-12-03 22:46

    What I did to sort out a similar problem is I created a new class that extended SearchView:

    public class EnglishVerbSearchView extends SearchView {
    
    OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
    
    public EnglishVerbSearchView(Context context) {
        super(context);
    }
    
    @Override
    public void onActionViewCollapsed() {
        if (mSearchViewCollapsedEventListener != null)
            mSearchViewCollapsedEventListener.onSearchViewCollapsed();
        super.onActionViewCollapsed();
    }
    
    public interface OnSearchViewCollapsedEventListener{
        public void onSearchViewCollapsed();
    }
    
    public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
        mSearchViewCollapsedEventListener = eventListener;
    }
    
    }
    

    You can then use this class instead of SearchView in your menu xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
        <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/action_search"
          yourapp:showAsAction="ifRoom|collapseActionView"
          yourapp:actionViewClass="com.szymontrapp.englishverbs.EnglishVerbSearchView" />
    </menu>
    

    And then you can add a listener in your activity:

        getMenuInflater().inflate(R.menu.dictionary, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        EnglishVerbSearchView searchView = (EnglishVerbSearchView)      MenuItemCompat.getActionView(searchItem);       
    
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
    
        searchView.setOnSearchViewCollapsedEventListener(listener);
    

    You can also override other methods in your class that replaces SearchView to achieve other goals.

    0 讨论(0)
  • 2020-12-03 22:55

    I ran into same problem on android 4.1.1. Looks like it is a known bug: https://code.google.com/p/android/issues/detail?id=25758

    Anyway, as a workaround i used state change listener (when SearchView is detached from action bar, it is also closed obviously).

    view.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
    
        @Override
        public void onViewDetachedFromWindow(View arg0) {
            // search was detached/closed
        }
    
        @Override
        public void onViewAttachedToWindow(View arg0) {
            // search was opened
        }
    });
    

    Above code worked well in my case.

    0 讨论(0)
  • 2020-12-03 23:01
        MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when action item collapses
                /*Hiding the mic on search visibility*/
                    myMenu.findItem(R.id.action_speak).setVisible(false);
                Log.v("test","colllapse");
                return true;  // Return true to collapse action view
            }
    
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // Do something when expanded
                Log.v("test","expand");
                return true;  // Return true to expand action view
            }
        };
    
        //Searchview Initilisation
        MenuItem searchViewItem = menu.findItem(R.id.action_search);
        // Assign the listener to that action item
        MenuItemCompat.setOnActionExpandListener(searchViewItem, expandListener);
    

    More info from here: https://developer.android.com/training/appbar/action-views.html

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