Android: Showing Action Bar menu items depending on ViewPager

前端 未结 4 1419
深忆病人
深忆病人 2021-01-02 01:09

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 s

4条回答
  •  失恋的感觉
    2021-01-02 02:07

    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();
    }
    

提交回复
热议问题