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

时间:2016-01-25 17:20:00

标签: android android-fragments android-actionbar searchview android-actionbar-compat

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

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

所以,如果有人可以提供帮助,我会很感激

2 个答案:

答案 0 :(得分:3)

所以我想出来了。根据我可以收集的内容,可以通过onOptionsItemSelected方法完成。

然后我在搜索视图上设置了一个onclicklistener,它也没有用。仔细检查后,我需要设置和onSearchClickListener,如下所示:

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

希望能帮助别人!

答案 1 :(得分: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;
    }