Java Code Examples for android.support.v7.widget.SearchView#setOnQueryTextFocusChangeListener()

The following examples show how to use android.support.v7.widget.SearchView#setOnQueryTextFocusChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MusicFragment.java    From TouchNews with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_music, menu);
    super.onCreateOptionsMenu(menu, inflater);
    mMenuItemSearch = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    mMenuItemSearch.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            mListView.setVisibility(View.GONE);
            //刷新待选List 为默认值
            updateSearchList(mSearchChooseDef);
            return false;
        }
    });
    //文本输入框可见、显示ListView
    mMenuItemSearch.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                mListView.setVisibility(View.VISIBLE);
            }
        }
    });
    mMenuItemSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //文本输入改变实时搜索关键字
            mMusicsPresenter.searchMusic(newText);
            //文本输入改变 List 滑动到顶部
            mListView.setSelection(0);
            return true;
        }
    });

}
 
Example 2
Source File: MainActivity.java    From memetastic with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.main__menu, menu);
    updateSearchFilter("");
    boolean isCreateTab = _bottomNav.getSelectedItemId() == R.id.nav_mode_create;
    menu.findItem(R.id.action_picture_from_camera).setVisible(isCreateTab);
    menu.findItem(R.id.action_picture_from_gallery).setVisible(isCreateTab);
    menu.findItem(R.id.action_search_meme).setVisible(isCreateTab);

    _searchItem = menu.findItem(R.id.action_search_meme);
    _searchView = (SearchView) _searchItem.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    _searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    _searchView.setQueryHint(getString(R.string.search_meme__appspecific));
    if (_searchView != null) {
        _searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (query != null) {
                    updateSearchFilter(query);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText != null) {
                    updateSearchFilter(newText);
                }
                return false;
            }
        });
        _searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                _searchItem.collapseActionView();
                updateSearchFilter("");
            }
        });
    }
    return true;
}
 
Example 3
Source File: MainActivity.java    From writeily-pro with MIT License 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    final MenuItem searchItem = menu.findItem(R.id.action_search);

    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    if (searchView != null) {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (query != null) {
                    if (notesFragment.isVisible())
                        notesFragment.search(query);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText != null) {
                    if (notesFragment.isVisible()) {
                        if (newText.equalsIgnoreCase("")) {
                            notesFragment.clearSearchFilter();
                        } else {
                            notesFragment.search(newText);
                        }
                    }
                }
                return false;
            }
        });

        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                menu.findItem(R.id.action_import).setVisible(false);
                menu.findItem(R.id.action_settings).setVisible(false);

                if (!hasFocus) {
                    menu.findItem(R.id.action_import).setVisible(true);
                    menu.findItem(R.id.action_settings).setVisible(true);
                    searchItem.collapseActionView();
                    searchView.setQuery("", false);
                }
            }
        });

        searchView.setQueryHint(getString(R.string.search_hint));
    }

    return true;
}