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

Juo*_*nis 11 android android-actionbar

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

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

VeV*_*VeV 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
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用AppCompat,则有"MenuItemCompat.setOnActionExpandListener" (3认同)

Juo*_*nis 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);
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)


Eri*_* B. 6

如果您正在使用MenuItemCompat:

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;
    }
});
Run Code Online (Sandbox Code Playgroud)