捕获搜索视图菜单项在片段中单击

问题描述 投票:0回答:2

我有一个附加在主要活动上的操作栏。活动托管有许多不同的片段。在操作栏中有菜单项。一种是搜索视图。我声明如下:

菜单xml。

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

在主要活动中:

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // Associate searchable configuration with the SearchView

        MenuItem item = menu.findItem(R.id.action_search);

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return super.onCreateOptionsMenu(menu);
    }

搜索视图起作用,当我单击图标时,搜索视图会展开。我需要捕获搜索视图的点击,因为当用户单击搜索视图时,我要显示一个向用户显示其最近搜索列表的视图。

我还将像这样使用搜索视图:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
            .getActionView();
    if (null != searchView) {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            //Here u can get the value "query" which is entered in the search box.

        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return super.onCreateOptionsMenu(menu);
}

并在用户键入3个或更多字符时执行搜索。

我在活动中尝试过此操作:

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        switch (item.getItemId()){
            case R.id.action_search:
                L.i("search", "search");
                return false;


            case android.R.id.home:
                L.i("home", "home pressed");
                break;

            case R.id.action_done:
                L.i("done", "done pressed");
                break;

            default:
                ;;
        }
        return super.onOptionsItemSelected(item);
    }

它识别完成的菜单项,但不识别搜索。我将需要能够获得点击的活动和片段。

因此,如果有人可以指出我要去哪里的地方。

哦,在我添加的片段中

setHasOptionsMenu(true);



@Override
    public boolean onOptionsItemSelected(MenuItem item) {


        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        switch (item.getItemId()){
            case R.id.action_search:
                L.i("search", "search");
                return false;


            case android.R.id.home:
                L.i("home", "home pressed");
                break;

            case R.id.action_done:

                //for testing
                checkSelectedInterests();
                //TODO: MLC
                L.i("done", "done pressed");
                break;

            default:
                ;;
        }
        return super.onOptionsItemSelected(item);
    }

所以如果有人可以帮助我,我将不胜感激

android android-fragments android-actionbar searchview android-actionbar-compat
2个回答
4
投票

所以我想通了。据我所知,这不能通过onOptionsItemSelected方法来完成。

因此,我在搜索视图上设置了一个onclicklistener,但该按钮也不起作用。经过仔细检查,我需要如下设置和onSearchClickListener:

searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.i(getClass().getSimpleName(), "SearchViewCLicked");
            }
        });

希望能帮助某人!


0
投票

这是一个简单的解决方案,将onActionExpandListener设置为菜单项,如下所示:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);

        // Get the MenuItem for the action item
        MenuItem actionMenuItem = menu.findItem(R.id.action_search);

        // Define the listener
        actionMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // Do what you want when it expands
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do what you want when it collapses
                return true;
            }
        });

        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.