I have a ViewPager with fragments in my Android app. I want to show Action Bar menu item depending the fragment of the ViewPager. I have readen some questions about this but I can´t find the correct solution. My ViewPager´s code is this:
public class My_Activity extends ActionBarActivity {
private ViewPager mViewPager;
String idioma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_main);
ActionBar actionBar = getSupportActionBar(); // || getActionBar();
actionBar.setIcon(getResources().getDrawable(R.drawable.navbar_logo));
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#CC3333")));
actionBar.setTitle("Hello");
mViewPager = (ViewPager) findViewById(R.id.viewpager);
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagerTabStrip);
pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.blanco));
Title_Liga_Adapter titleAdapter = new Title_Liga_Adapter(getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);
mViewPager.setCurrentItem(0);
getSupportActionBar().setIcon(R.drawable.navbar_logo);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (mViewPager.getCurrentItem()==4){
getMenuInflater().inflate(R.menu.menu_comunidad, menu);
}
else{
}
return true;
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_search).setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true;
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I want that when CurrentItem is 4 for example show menu items icons.
First create addOnPageChangeListener in onCreate method
Then you override onCreateOptionMenu()
OK..you will have to play with the ViewPager.OnPageChangeListener where you will monitor the method onPageSelected() that will be triggered whenever you change the page of the ViewPager. You will have to keep track of what is the current page AND dispatch a call to the ActivityCompat.invalidateOptionsMenu every time the onPageSelected() is called. This invalidateOptionsMenu() is the key to make it work, it will be responsible for triggering the 'refresh' of options menu..that will indirectly call the onCreateOptionsMenu() method will be called and you have the opportunity to 'hide' or 'show' a different set of items according to the selected tab.
For instance:
I answer my own question: