SearchView onMenuItemActionCollapse Not Working

落爺英雄遲暮 提交于 2019-12-25 12:26:00

问题


I'm using SearchView while my activity is extanding Sherlock Library.

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub

        inflater.inflate(R.menu.countriesxml, menu);
        SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
                .getActionView();
        mSearchView.setQueryHint("Search for smartphones");
        MenuItem menuItem = menu.findItem(R.id.searchall);

menuItem.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return false;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // TODO Auto-generated method stub
            item.collapseActionView();
            indicator.setVisibility(View.VISIBLE);
            pager.setVisibility(View.VISIBLE);
            lvth.setVisibility(View.GONE);
            return true;
        }
    });

    return;
}

Please note that i tried to use OnCloseListener() but it didn't work.

What's wrong?


回答1:


have a look at your code at this section:

@Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return false;
    }

Replace it with below code and try again.

@Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }



回答2:


Try using OnFocusChangeListener instead

    menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                // Code here
            }
        }
    });


来源:https://stackoverflow.com/questions/20869445/searchview-onmenuitemactioncollapse-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!