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

后端 未结 2 1166
清歌不尽
清歌不尽 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.

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menu_item_search"
            android:icon="@drawable/ic_action_search"
            android:title="@string/description_search"
            android:orderInCategory="1"
            android:showAsAction="ifRoom" />
        <item android:id="@+id/menu_item_share"
            android:icon="@drawable/ic_action_share"
            android:title="@string/description_share"
            android:orderInCategory="1"
            android:showAsAction="ifRoom" />
    </menu>
    

    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.

    0 讨论(0)
  • 2021-02-04 13:18

    Yes, I got the output with these codes ,

    In TabOne.java and TabTwo.java I've added

       setHasOptionsMenu(true);
    

    In onCreateView() function and after that menu function with different menu,removed menu function from main class.

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState)
        {
                View view = inflater.inflate(R.layout.tab_a, container, false);
                setHasOptionsMenu(true);
                return view;
        }
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // TODO Auto-generated method stub
            //super.onCreateOptionsMenu(menu, inflater);
             inflater.inflate(R.menu.menu_b, menu);
        }
    
    0 讨论(0)
提交回复
热议问题