-1

我正在尝试为MenuItems我的每个应用程序的选项卡(ActionBar 导航选项卡)设置不同的设置。我尝试了很多解决方案,但最后一个似乎是最接近的。当我打开我的应用程序时,它会隐藏我的MenuItem,但是当我滑动到第二个 Fragment 时,它不会显示MenuItem,尽管我设置它必须是可见的。

你有什么想法,如何解决这个问题?

请参阅下面的代码(请主要检查注释“ Here ”附近的语句):

public class MainActivity extends ActionBarActivity{
    ProgressBar mProgressBar;
    EditText mEditText;
    RelativeLayout mRelativeLayout;
    ProgressBar oProgressBar;
    LinearLayout mLinearLayout;
    ViewPager mPager;
    ActionBar mActionBar;
    Menu mMenu;
    Tab mTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Parse.initialize(this, "code", "code");

        ParseObject.registerSubclass(Animal.class);
        ParseAnalytics.trackAppOpened(getIntent());
        Log.v("Initialize", "intitalizing from Parse.com app");

        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mPager = (ViewPager) findViewById(R.id.pager);

        ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                mActionBar.setSelectedNavigationItem(position);
            }
        };

        FragmentManager fm = getSupportFragmentManager();
        mPager.setOnPageChangeListener(ViewPagerListener);

        ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);

        mPager.setAdapter(viewpageradapter);



        ActionBar.TabListener tabListener = new ActionBar.TabListener() {


            @Override
            public void onTabReselected(Tab mTab, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab mTab, FragmentTransaction arg1) {
                 mPager.setCurrentItem(mTab.getPosition());
                 mPager.setOffscreenPageLimit(3);
                 supportInvalidateOptionsMenu();  //Here


            }

            @Override
            public void onTabUnselected(Tab mTab, FragmentTransaction arg1) {
                // TODO Auto-generated method stub
                supportInvalidateOptionsMenu();
            }
        };



        ActionBar.Tab Animals = mActionBar.newTab().setText("Animals");
        ActionBar.Tab Frag2Tab = mActionBar.newTab().setText("Favorites");
        ActionBar.Tab Frag3Tab = mActionBar.newTab().setText("Settings");


        Animals.setTabListener(tabListener);
        Frag2Tab.setTabListener(tabListener);
        Frag3Tab.setTabListener(tabListener);


        mActionBar.addTab(Animals);
        mActionBar.addTab(Frag2Tab);
        mActionBar.addTab(Frag3Tab);

    }



    @Override
     public void onDestroy(){
         System.exit(0);
     }


    @Override
public boolean onPrepareOptionsMenu(Menu menu) {

    super.onPrepareOptionsMenu(menu);
        Fragment1 frg1 = new Fragment1();
        Fragment2 frg2 = new Fragment2();
        Fragment3 frg3 = new Fragment3();


        try {
            if(frg1.isMenuVisible()){


                    menu.findItem(R.id.action_refresh).setEnabled(false) .setTitle("").setIcon(null);
                    Log.v("fdsfad", "sadfasdfsa");

                            }
            else if (frg2.isMenuVisible()){


                    menu.findItem(R.id.action_refresh).setEnabled(true) .setTitle("Refresh").setIcon(R.drawable.ic_action_refresh);
                    Log.v("fdsfad", "sadfasdfsa");

                            }

            else if (frg3.isMenuVisible()){


                menu.findItem(R.id.action_refresh).setEnabled(true) .setTitle("Refresh").setIcon(R.drawable.ic_action_refresh);
                    Log.v("fdsfad", "sadfasdfsa");

                            }

    }   catch (Exception e) {
                    e.printStackTrace();
                                            }


        return true;

}
     @Override
        public boolean onCreateOptionsMenu(Menu menu) {


            getMenuInflater().inflate(R.menu.main, menu);

            return true;
        }




         public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_refresh:
                    Toast.makeText(getBaseContext(), "Pull to refresh", Toast.LENGTH_SHORT).show();

                    return true; 
                 } 
            return super.onOptionsItemSelected(item);
         }



         @Override
            public boolean onKeyDown (int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                    Toast.makeText(this, "Search button: clicked", Toast.LENGTH_SHORT).show();
                }

                return super.onKeyDown(keyCode, event);
            }




         }

提前致谢!

4

1 回答 1

2

如果没有更多项目可见, setVisible(true) 将不起作用。

一种选择是使图标为空,设置空标题并禁用此项目。或者,在 onPrepareOptionsMenu 中清除和膨胀菜单可能会运行良好。

于 2014-02-01T22:45:17.030 回答