简体   繁体   English

将菜单添加到活动中而不是片段

[英]Adding a menu to an activity than a fragment

I have a MainActivity along with a fragment. 我有一个MainActivity以及一个片段。 I have a refresh menu option that will refresh the data (Using AsyncTask ). 我有一个刷新菜单选项,它将刷新数据(使用AsyncTask )。 Is there any difference in inflating the refresh menu option in the fragment than in the MainActivity? 在片段中增加刷新菜单选项与在MainActivity中是否有区别?

Note : This is in context with Udacity's Developing Android Apps, Lesson 2. 注意 :这是与Udacity的开发Android应用程序第2课相关的。

Yes, if i understand right, you want to inflate your activity toolbar menu from the the fragment. 是的,如果我理解正确,您想从片段中添加活动工具栏菜单。 If that is the case you can do it like this 如果是这样,您可以这样做

Yout Fragment class Yout Fragment类

   @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setHasOptionsMenu is important 
            //it's telling the parent activity that he wants to participate in inflation of the menu
            setHasOptionsMenu(true);
        }



        //Rest of your methods (onCreateView, onPause, onResume etc...)


        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            //inflate the menu file
            inflater.inflate(R.menu.your_menu_xml, menu);
            super.onCreateOptionsMenu(menu, inflater);
        }

        @Override
        public boolean onOptionsItemSelected(android.view.MenuItem item) {

            switch (item.getItemId()) {

                case R.id.refresh:
                    //handle click
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }

You can also use setMenuVisibility(boolean) if you want to hide/show menu in certain childfragments 如果要在某些子片段中隐藏/显示菜单,也可以使用setMenuVisibility(boolean)

If you inflate the menu from within the activity, it will be shown in all of the fragments you load inside the activity, but the action will be available to activity only (This is good if you want to do some general stuff with your menu action like starting new activity, displaying information popup etc). 如果您在活动中为菜单充气,它将显示在活动中加载的所有片段中,但是该操作仅对活动可用(如果您想对菜单操作进行一些常规操作,这很好例如开始新活动,显示信息弹出窗口等)。 If you inflate the menu from within the fragment you will be able to handle menu items from withing the fragment, which will allow you to create more specific menu actions based on which fragment is currently active. 如果您从片段中为菜单充气,那么您将能够处理该片段中的菜单项,这将使您能够基于当前活动的片段来创建更具体的菜单操作。 For example if you have viewpager with 3 different fragments lets say: 例如,如果您的Viewpager具有3个不同的片段,则可以说:

  • FragmentOne for image browsing 用于图像浏览的FragmentOne
  • FragmentTwo for video browsing FragmentTwo用于视频浏览
  • FragmentThre for Text browsing FragmentThre用于文本浏览

Lets say you want to allow users to upload the images only, and you want that upload button to be located in the menu. 假设您要允许用户仅上传图像,并且希望该上传按钮位于菜单中。

If you inflate the the menu from within the activity your upload button will be visible inside all of your fragments, and you would have to create a custom/logic for showing hiding menu items. 如果您在活动中对菜单进行充气,则所有片段中都将显示您的上传按钮,您将必须创建一个自定义/逻辑来显示隐藏的菜单项。 If you create a menu from within the fragment, you will be able to handle and show the menu for the fragment you need 如果您从片段中创建菜单,则可以处理并显示所需片段的菜单

Long story short i think this depends on the use case of the activity/fragment and what you want to achieve with it 长话短说,我认为这取决于活动/片段的用例以及您要实现的目标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM