SearchView's OnCloseListener doesn't work

前端 未结 18 2331
有刺的猬
有刺的猬 2020-11-27 02:44

I\'m trying to add support for the SearchView in the Android 3.0+ ActionBar, but I can\'t get the OnCloseListener to work.

Here\'s my code:

相关标签:
18条回答
  • 2020-11-27 03:26

    For Android API 14+ (ICS and greater) use this code:

    // When using the support library, the setOnActionExpandListener() method is
    // static and accepts the MenuItem object as an argument
    MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }
    
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    });
    

    For more information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

    Ref: onActionCollapse/onActionExpand

    0 讨论(0)
  • 2020-11-27 03:29

    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.


    I post same answer here: https://stackoverflow.com/a/24573266/2162924

    0 讨论(0)
  • 2020-11-27 03:31

    In order to make the OnCloseListener work, make sure that showAsAction is set to always in the search menu item.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context=".SearchActivity">
    
        <item
            android:id="@+id/search"
            android:title="@string/search"
            android:icon="@drawable/ic_search_toolbar"
            app:showAsAction="always"
            app:actionViewClass="android.support.v7.widget.SearchView"/>
    </menu>
    
    0 讨论(0)
  • 2020-11-27 03:31
        searchView.setOnCloseListener {
            d("click", "close clicked")
            return@setOnCloseListener false
        }
    

    if you click on close searchView ->

    D/click: close clicked

    0 讨论(0)
  • 2020-11-27 03:34

    Well, this solved my problem:

    Menu item with showAsAction="always"

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>
    

    and in activity

    searchView.setOnCloseListener(new OnCloseListener() {
    
            @Override
            public boolean onClose() {
    
                Log.i("SearchView:", "onClose");
                searchView.onActionViewCollapsed();
                return false;
            }
        });
    
    0 讨论(0)
  • 2020-11-27 03:37

    For MenuItemCompat problem I added ViewTreeObserver to track the visibility state. You can check my answer here: https://stackoverflow.com/a/28762632/1633609

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