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

发布于 2024-12-14 08:26:27 字数 289 浏览 1 评论 0原文

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

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

What event(s) I should listen to determine when user clicks on iconified SearchView. I want to remove some items (ActionBar navigation tabs, if that is important) from action bar to make more space in portrait orientation.

I've tried OnClickListener, OnFocusChangeListener, OnTouchListener and other events but neither gets triggered by SearchView expansion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛泛之交 2024-12-21 08:26:27

从 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
        }
    });
}

Since API Level 14 you have a dedicated listener:
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
        }
    });
}
何处潇湘 2024-12-21 08:26:27

我找到了一种使用 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);
            }
        }
    };

I found a way to get that event using 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);
            }
        }
    };
み青杉依旧 2024-12-21 08:26:27

如果您使用的是 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;
    }
});

If you're using 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;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文