当菜单项的可见性设置为false时,它会留下空白

时间:2015-02-07 11:06:52

标签: android menu android-actionbar searchview

我有一个功能,我需要在 searchView 展开时显示一个菜单项,并在 searchView 关闭时消失。展开时,项目的 setVisible 为false,它会使菜单项在返回到 searchView 的折叠状态时消失,但会留下空白。

截图:

searchView 图标是否可以返回其原始位置?

SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    final MenuItem menuitem=menu.findItem(R.id.action_search);
    final MenuItem locationitem=menu.findItem(R.id.action_location).setVisible(false);
    SearchView searchView = (SearchView) menuitem.getActionView();
    if(null!=searchManager ) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    searchView.setIconifiedByDefault(true);   //if using on actionbar
    searchView.setClickable(true);
    searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
        @Override
        public boolean onSuggestionClick(int position) {
            // Your code here
            return true;
        }

        @Override
        public boolean onSuggestionSelect(int position) {
            // Your code here
            return true;
        }
    });
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            populateAdapter(s);
            return false;
        }
    });
    searchView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            SearchView searchView = (SearchView) v;
            if (searchView.isIconified()) {

                locationitem.setVisible(false);

                Log.d("Trial","Working!!");
                fr=new Fragment_Two();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                ft.replace(R.id.fragmentswitcher,fr);
                ft.commit();
            }
            else{
                locationitem.setVisible(true);
                Log.d("Trial","Working too!!");
                MenuItemCompat.setShowAsAction(menuitem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
                MenuItemCompat.setShowAsAction(locationitem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
                fr=new Fragment_One();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                ft.replace(R.id.fragmentswitcher,fr);
                ft.commit();
                View newView=fr.getView();

            }
        }
    });

1 个答案:

答案 0 :(得分:0)

不幸的是,使用setVisible()将隐藏菜单项,但是菜单项仍会占用空间。您也不能强制转换为View对象并使用setVisibility()而不会崩溃。我找不到为什么的文档,但是我确信它是故意设计的。

要获得有时不出现菜单项的结果,您需要使用onPrepareOptionsMenu()。在此函数中,根据您喜欢的逻辑隐藏不想显示的菜单项,并在每次重绘菜单时使用方法invalidateOptionsMenu()。参见示例:

public void someFunction() {
    invalidateOptionsMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (itemRemovalNeeded) {
        menu.removeItem(R.id.myItem);
    }
    return true;
}

参考: Android Activity Documentation