Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionbarSherlock and ViewpagerIndicator: how to add different actionbar menu to different Page in ViewPagerIndicator using fragment

My app use an Actionbarsherlock with a ViewpagerIndicator. Each page using 1 fragment. I want each page will have different actionbar menu, so how can I archive this?

I have tried using onCreateOptionsMenu on each SherlockFragment but the actionbar menu not show, I also tried finding some methods to add menu programmatically but no way.

@Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater)
{       
    inflater.inflate(R.menu.main, menu);    
    super.onCreateOptionsMenu(menu, inflater);
}

Thanks you!

*EDIT: Add more details *

If i use onCreateOptionsMenu in the Activity, the menu (action item) show normally.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getSupportMenuInflater();
    menuInflater.inflate(R.menu.main, menu);

This is how I set up the ActionBar the the ViewPager

final ActionBar actionbar = getSupportActionBar();
    actionbar .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionbar .setDisplayHomeAsUpEnabled(true);
    actionbar .setTitle("olala");

    mFragmentAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());     
    mPager.setAdapter(mFragmentAdapter);

    mIndicator.setViewPager(mPager);
    mIndicator.setOnPageChangeListener(new MyPageChangeListener());
    mPager.setCurrentItem(3);
like image 872
Wayne Avatar asked Dec 22 '25 23:12

Wayne


1 Answers

We need to add this on Fragment (onCreate or onCreateView) in order to report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.

setHasOptionsMenu(true);

ref: http://developer.android.com/reference/android/app/Fragment.html

like image 86
Wayne Avatar answered Dec 28 '25 16:12

Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!