如何在不同的片段中更改选项菜单?

时间:2014-04-28 10:41:57

标签: java android android-fragments android-actionbar android-menu

我有一个带菜单的片段:

public class FragmentA extends Fragment {

    public FragmentA() {
        setHasOptionsMenu(true);
    }

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

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

我想更改菜单,但它不起作用并保留旧的操作菜单

片段B与上面的相同,具有不同的膨胀XML菜单。

public class FragmentB extends Fragment {

    public FragmentB() {
        setHasOptionsMenu(true);
    }

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

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

已编辑:

对于不同的片段使用不同的菜单布局,对主要活动和不同的id

使用1个菜单布局会很有用

3 个答案:

答案 0 :(得分:27)

setHasOptionsMenu(true)置于构造函数中并膨胀特定于片段的菜单。

public class FragmentA extends Fragment {

    public FragmentA() {
       setHasOptionsMenu(true);
    }

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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragmenta_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
}
主要活动中的

菜单

public class MainActivity extends Activity {
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
}

答案 1 :(得分:1)

所有操作都可以通过Fragment完成-无需在活动中添加菜单:

public class UpdateFragment extends Fragment  {

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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.update_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.navUpdateProfile:
                showToast("navUpdateProfile");
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

答案 2 :(得分:0)

如果您有多个片段共享同一个菜单,但有一些例外。

  class BaseFragment:Fragment(){
    
    open var menuId = R.menu.default_menu
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setHasOptionsMenu(true) // will apply to all children
        }
        override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
            super.onCreateOptionsMenu(menu, inflater)
            inflater.inflate(menuId, menu) // will apply to all children except for overridden
        }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
            // all menu ids can be listed here unless specific to code in child
            when (item.itemId) {
                R.id.menu_option_1 -> {
                      // do something
                }
                R.id.menu_option_2 -> {
                    //do something
            }
            return false
        }
    }
    
    class ChildFragment:BasFragment(){
    
 override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            menuId = R.menu.menu_2 // change to a different menu as desired here
        }
    }