添加菜单项动态地从SherlockActionBar片段标签 [英] Adding Menu Item dynamically from SherlockActionBar Fragment Tabs

查看:112
本文介绍了添加菜单项动态地从SherlockActionBar片段标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在工作和Android应用程序,有一个导航栏上有几个选项卡上,而该部分工作正常,但现在我希望能够动态地从不同的片段添加菜单项的操作栏(因为一些片段可以具有不同的可用选项)。到目前为止,不管是什么我已经试过我似乎无法得到onCreateOptionsMenu被调用。以下是我迄今为止

So I've been working on and Android app that has a Navigation Bar on the top with several Tabs, and that part is working fine but now I want to be able to dynamically add Menu Items to the Action Bar from different Fragments (since some Fragments may have different options available). So far no matter what I've tried I can't seem to get the onCreateOptionsMenu to be called. Here's what I have so far

//First I have a holder class that is used to navigate between the different Fragment Tabs
 public class ActionHolder extends SherlockFragmentActivity implements ActionBar.TabListener {....
//And then I have this method for switching Fragments based on what Tab is selected
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    int selectedTab = tab.getPosition();

    if (selectedTab == 0) {
        SalesMainScreen salesScreen = new SalesMainScreen();
        ft.replace(R.id.content, salesScreen);
    }
    else if (selectedTab == 1) {
        ClientMainScreen clientScreen = new ClientMainScreen();
        ft.replace(R.id.content, clientScreen);
    }.....

现在这里是标签的片段(该SalesMainScreen),我想有添加到操作栏

Now here is one of the Tab's Fragments (the SalesMainScreen) that I want to have a few menu items added to the Action Bar

 @Override
public void onCreate (Bundle savedInstanceState) {
    Log.i("message","the oncreate method was called");
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
    return inflater.inflate(R.layout.salesmainscreen, group, false);
}

@Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.i("message", "the oncreatemenu method called");
    inflater.inflate(R.menu.menu_refresh, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

我看到被称为OnCreate中日志消息,但我没有看到onCreateOptionsMenu日志被调用的。另外,我知道,有时进口造成问题,但是当我导入福尔摩斯菜单和菜单充气我得到的各种错误信息的OnCreateOptionMenu方法对他们不被兼容。是否有可能在此设置动态添加菜单项的操作栏,或者我应该只是添加的项目,然后就不要做那些并不适​​用于正在显示片段的任何行动?

I see the OnCreate Log message being called but I don't see the onCreateOptionsMenu Log being called at all. Also, I know that sometimes the imports cause issues, but when I import the Sherlock Menu and Menu Inflater I get all kinds of error messages on the OnCreateOptionMenu method about them not being compatible. Is it possible in this setup to dynamically add Menu Items to the Action Bar, or should I just add the items and then just don't do any actions on the ones that don't apply to the fragment that is being displayed?

推荐答案

我在使用 SherlockActionBar 的应用程序和标签,其中包含每个选项卡 SherlockFragment 。主要活动有自己的菜单操作栏中,而片段中的一个增加了一个搜索项的操作栏菜单。

I have an app using SherlockActionBar and tabs, with each tab containing a SherlockFragment. The main activity has its own menu in the action bar, and one of the fragments adds a search item to the action bar menu.

主要活动有以下内容:

class MainActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...        
        ActionBar bar = getSupportActionBar();
        bar.addTab(createThingOneTab());
        bar.addTab(createThingTwoTab());
        bar.addTab(createThingThreeTab());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    }

}

在选项卡中的片段,有以下几点:

The fragment in the tab has the following:

class ThingOneFragment extends SherlockFragment {

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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        MenuItem search = menu.add("Search");
        search.setIcon(android.R.drawable.ic_menu_search);
        search.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        ...
    }

}

当我开始的主要活动,该选项卡显示 ThingOneFragment 默认情况下,我看到在操作栏上的搜索。当我选择了其他选项卡,搜索图标消失。你需要确保你使用的是大侦探类菜单 MenuInflater 等。

When I start the main activity, the tab shows ThingOneFragment by default and I see the search icon in the action bar. When I select the other tabs, the search icon disappears. You do need to make sure that you are using the Sherlock classes for Menu, MenuInflater, etc.

我不知道这是否有差别,但我的 TabListener 是这样的:

I'm not sure if it makes a difference but my TabListener looks like this:

private TabListener createTabListener(final Class<? extends Fragment> clazz) {
    return new TabListener() {

        private Fragment mFragment;

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // no action
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            if (mFragment == null) {
                mFragment = Fragment.instantiate(activity, clazz.getName());
            }
            getSupportFragmentManager().beginTransaction()
                                       .replace(android.R.id.content, mFragment)
                                       .commit();
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // no action
        }
    };

我不知道这是造成你的问题,或者如果它甚至处理选项卡上的正确的方式,但我有它的完整性。

I'm not sure if that is causing your issue or if it's even the correct way of handling tabs but I include it for completeness.

这篇关于添加菜单项动态地从SherlockActionBar片段标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆