Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Showing Action Bar menu items depending on ViewPager

I am having trouble getting the following piece of code to work out. I have a viewpager with 3 fragments, and I want a search icon to only show up on one. I started off trying to add the search function by the fragment, but the rendering of the menu item was slow when swiping to that page. I am now on the part to add the search icon to the activity, and then just hide or show depending on which viewpager page is active, but the following is not working:

public class MyApp extends FragmentActivity implements 
   FragmentTeams.FragmentNotification,ViewPager.OnPageChangeListener, 
      OnNavigationListener{

  ...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    menuSearch = menu.findItem(R.id.menu_search);
    mSearchView = new SearchView(this);
    menuSearch.setActionView(mSearchView);
    menuSearch.setVisible(false);
    return true;
}

 @Override
public void onPageSelected(int pageNum) {

if(pageNum== 1){     
    ActionBar actionBar = MyApp.this.getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);     
        menuSearch.setVisible(true);
        invalidateOptionsMenu();

}else{           
    ActionBar actionBar = MyApp.this.getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
        menuSearch.setVisible(false);
        invalidateOptionsMenu();
    }
}

While the above does (appear to) create and hide the icon at onCreateOptionsMenu, it is not reenabled when moving to

 pageNum ==1  

Can anyone give me some insight as to why this may be happening?

like image 930
Josh Avatar asked Nov 05 '12 10:11

Josh


People also ask

What is Androidx ViewPager widget ViewPager?

A viewpager with touch and key event handling disabled by default. Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

How do I know if my ViewPager is scrolling left or right Android?

You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() . If the current page is less than or equal to the position , you are scrolling to the right, if not, you are scrolling to the left.

Can we add activity in ViewPager?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .

How do I use ViewPager on Android?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.


2 Answers

invalidateOptionsMenu make the system calls the method onPrepareOptionsMenu, so you can override this method as follows:

public boolean onPrepareOptionsMenu(Menu menu) {
      int pageNum = getCurrentPage();
      if (pageNum == 1) {
         menu.findItem(R.id.menu_search).setVisible(true);

      }
      else {
         menu.findItem(R.id.menu_search).setVisible(false);
      }
   }

public void onPageSelected(int pageNum) {
    invalidateOptionsMenu();
}
like image 69
Nermeen Avatar answered Oct 21 '22 04:10

Nermeen


You can implement onCreateOptionsMenu() in your Fragment and set 'setHasOptionsMenu(true)' for the fragment

like image 22
Boy Avatar answered Oct 21 '22 02:10

Boy