OptionsMenu of Fragments in Viewpager showing each

2019-01-22 15:06发布

问题:

I've got three fragments in a viewpager.

Two of these fragments have their own version of the onCreateOptionsMenu method:

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    // Set up 1 action button
    inflater.inflate(R.menu.home_snapshot_add, menu);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    // Set up 2 action buttons
    inflater.inflate(R.menu.home_snapshot_send, menu);
}

The home activity has a basic onCreateOptionsMenu method:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    return false;
}

In the onCreate method, each fragment calls the method:

setHasOptionsMenu(true);

Each of the menu items have the tag:

android:showAsAction="always"

Seems like as I open the Activity, all three buttons appear. However, when I scroll through them, the wrong ones magically disappear. It feels like the activity is calling every Fragment's options menu on Activity creation and then changes the menu appropriately when I swipe left and right. I've checked the menus but not sure what's wrong. Anything you reckon I need to check? I'm a little out of ideas.

Thanks!

回答1:

In your ViewPager's OnPageChangeListener and after setting the adapter to the ViewPager, have this:

@Override
public void onPageSelected(int position){
   invalidateFragmentMenus(position);
}


private void invalidateFragmentMenus(int position){
   for(int i = 0; i < mViewPagerFragentAdapter.getCount(); i++){
      mViewPagerAdapter.getItem(i).setHasOptionsMenu(i == position);
   }
   invalidateOptionsMenu(); //or respectively its support method.
}

After setting your fragment adapter call the same method with following argument:

invalidateFragmentMenus(mViewPager.getCurrentItem());

The above statements will prevent all other fragments not to receive call on onCreateOptionsMenu() method when invalidateOptionsMenu() is called, only the currently visible fragment will receive and be able to populate the options menu.



回答2:

I've used this and it has worked for me:

//In your Fragment
@Override
public void onResume() {
    super.onResume();
    setHasOptionsMenu(isVisible());

}


回答3:

OnPageChangeListener is deprecated with addOnChangeListener .

So try to use

yourViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               // do your operation here.
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });