searchview从图标化视图展开时的事件

时间:2011-11-05 11:18:51

标签: android android-actionbar

当用户点击图标化SearchView时,我应该听取哪些事件。我想从操作栏中删除一些项目(ActionBar导航标签,如果这很重要),以便在纵向方向上腾出更多空间。

我尝试过OnClickListener,OnFocusChangeListener,OnTouchListener和其他事件,但都没有被SearchView扩展触发。

3 个答案:

答案 0 :(得分:26)

从API Level 14开始,您就拥有了一个专用的监听器: http://developer.android.com/guide/topics/ui/actionbar.html

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options, menu);
        MenuItem menuItem = menu.findItem(R.id.actionItem);
        ...

    menuItem.setOnActionExpandListener(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
        }
    });
}

答案 1 :(得分:14)

我找到了使用addOnLayoutChangeListener

获取该事件的方法
private final OnLayoutChangeListener _searchExpandHandler = new OnLayoutChangeListener()
    {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
            int oldBottom)
        {
        SearchView searchView = (SearchView)v;
        if (searchView.isIconfiedByDefault() && !searchView.isIconified())
            {
            // search got expanded from icon to search box, hide tabs to make space
            getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            }
        }
    };

答案 2 :(得分:6)

如果您正在使用MenuItem searchMenuItem = menu.findItem(R.id.action_search); MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { Log.d("TAG", "Collapsed"); return true; } @Override public boolean onMenuItemActionExpand(MenuItem item) { Log.d("TAG", "Expanded"); return true; } });

mine