Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to change menu item at page scrolling with ViewPager?

The last version of WhatsApp does what I'm talking about. When you scroll between tabs the toolbar shows different menu options. I can implement ViewPager with adapter and fragments but not this. How could it be done? I really don't know what kind of trick is behind. It changes everytime you switch pages

like image 635
user3290180 Avatar asked Jun 04 '15 16:06

user3290180


1 Answers

In each Fragment you have to override onCreateOptionsMenu, with different menu/menu.xml files

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

Don't forget to call setHasOptionsMenu(true);, in onCreate of your Fragment

like image 141
Blackbelt Avatar answered Oct 24 '22 07:10

Blackbelt