SearchView's OnCloseListener doesn't work

前端 未结 18 2559
有刺的猬
有刺的猬 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 03:11

    For this problem I came up with something like this,

    private SearchView mSearchView;
    
    @TargetApi(14)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.conversation_index_activity_menu, menu);
    
        mSearchView = (SearchView) menu.findItem(R.id.itemSearch).getActionView();
    
        MenuItem menuItem = menu.findItem(R.id.itemSearch);
    
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
            menuItem.setOnActionExpandListener(new OnActionExpandListener()
            {
    
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item)
                {
                    // Do something when collapsed
                    Log.i(TAG, "onMenuItemActionCollapse " + item.getItemId());
                    return true; // Return true to collapse action view
                }
    
                @Override
                public boolean onMenuItemActionExpand(MenuItem item)
                {
                    // TODO Auto-generated method stub
                    Log.i(TAG, "onMenuItemActionExpand " + item.getItemId());
                    return true;
                }
            });
        } else
        {
            // do something for phones running an SDK before froyo
            mSearchView.setOnCloseListener(new OnCloseListener()
            {
    
                @Override
                public boolean onClose()
                {
                    Log.i(TAG, "mSearchView on close ");
                    // TODO Auto-generated method stub
                    return false;
                }
            });
        }
    
    
        return super.onCreateOptionsMenu(menu);
    
    }
    

提交回复
热议问题