无法使用StandAlone工具栏中实现的SearchView进行搜索

时间:2015-09-23 06:26:22

标签: android android-fragments android-5.0-lollipop searchview android-toolbar

我尝试在片段中的独立SearchView中实现Toolbar但无法获得任何结果。

我按照这个回答SearchView at a secondary Toolbar但没有成功。

这是我的实施

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
        initViews(rootView);
        initBottomToolbar(rootView);
        return rootView;
    }

    private void initBottomToolbar(View rootView) {
        mToolbarBottom = (Toolbar) rootView.findViewById(R.id.toolbar_bottom);
        // Set an OnMenuItemClickListener to handle menu item clicks
        mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                // Handle the menu item
                Toast.makeText(getActivity(), "action_search clicked", Toast.LENGTH_SHORT).show();

                SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
                SearchView searchView = null;

                if (menuItem != null) {
                    Log.d("Bottom Toolbar", "menuItem != null");
                    searchView = (SearchView) menuItem.getActionView();
                }

                if (searchView != null) {
                    Log.d("Bottom Toolbar", "searchView != null");
                    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

                    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT).show();
                            Log.e("Bottom Toolbar", "Submitted: "+query);
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String newQuery) {
                            Toast.makeText(getActivity(), newQuery, Toast.LENGTH_SHORT).show();
                            Log.d("Bottom Toolbar", "Changed: " + newQuery);
                            List<ContactsBean> filteredContactsBeanList = filter(mContactsBeanList, newQuery);
                            mContactsAdapter.animateTo(filteredContactsBeanList);
                            mContactsRecyclerView.scrollToPosition(0);
                            return true;
                        }
                    });
                }
                return true;
            }
        });

        // Inflate a menu to be displayed in the toolbar
        mToolbarBottom.inflateMenu(R.menu.menu_bottom_toolbar);

    }

即使如果我点击SearchView,它也既没有显示Toast也没有在日志中显示任何消息。

1 个答案:

答案 0 :(得分:0)

我通过在绑定侦听器

之前首先在我的toolbarBottom上放置菜单来实现它
toolbarBottom.inflateMenu(R.menu.menu_contacts_bottom);
    if(toolbarBottom.getMenu().size() > 0){
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(
            Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = toolbarBottom.getMenu().getItem(toolbarBottom.getMenu().size()-2);
        mSearchView = (SearchView) searchMenuItem.getActionView();
        if (mSearchView != null) {
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        }

        mSearchView.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {
                Log.d(TAG, "onClose");
                return false;
            }
        });

        mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Log.d(TAG, "query: " + newText);
                return false;
            }
        });
    }