Different menu for different tabs in tab+swipe application for android project

后端 未结 2 1167
清歌不尽
清歌不尽 2021-02-04 12:17

I am a beginner to android applications and java, basically I am a PHP developer.

I\'ve a project for a tab+swipe application,

Reseller.java

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 12:53

    create a menu xml file for menu_a.xml and menu_b.xml in the res/menu folder.

    
        
        
    
    

    To create a option menu for the current Fragment displayed, add setHasOptionsMenu(true); to the Fragment onCreate() method.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setHasOptionsMenu(true);
    
    }
    

    Then you must inflate the corresponding option menu (menu_a.xml or menu_b.xml) by override the onCreateOptionsMenu() method.

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

    For handling menu selections override the onOptionsItemSelected()

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
            case R.id.menu_item_search:                
                  //do something
                return true;
    
            case R.id.menu_item_share:
                  //do something
                return true;
        }
        return super.onOptionsItemSelected(item);
    } 
    

    see Creating an Options Menu: http://developer.android.com/guide/topics/ui/menus.html for more details.

提交回复
热议问题