ActionbarSherlock and ViewpagerIndicator: how to add different actionbar menu to different Page in ViewPagerIndicator using fragment
-
10-06-2021 - |
Question
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);
Solution
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
OTHER TIPS
I have the same without the call to super
.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.chat_thread_list_fragment_menu, menu);
}
Update:
I have set this in my onCreate of the Activity...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_activity);
final ViewPager viewPager = (ViewPager) findViewById(R.id.profile_activity_pager);
final Intent intent = getIntent();
// I guess some setup should be done...
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
bar.setDisplayHomeAsUpEnabled(true);
if (intent.hasExtra(EXTRA_USER_NAME)) {
bar.setTitle(intent.getStringExtra(EXTRA_USER_NAME));
} else {
bar.setTitle(R.string.title_profile);
}
final PagerAdapter tabsAdapter = new TabsAdapter(this);
viewPager.setAdapter(tabsAdapter);
final TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.profile_activity_tabs);
titleIndicator.setViewPager(viewPager);
if (intent.hasExtra(EXTRA_TAB_ID)) {
final int tabId = intent.getIntExtra(EXTRA_TAB_ID, -1);
if (tabId > -1 && tabId < tabsAdapter.getCount()) {
viewPager.setCurrentItem(tabId);
}
}
}