Adding Menu Item dynamically from SherlockActionBa

2019-03-31 13:58发布

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);
    }.....

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);
}

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?

1条回答
我命由我不由天
2楼-- · 2019-03-31 14:57

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.

The main activity has the following:

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);
        ...
    }

}

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.

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.

查看更多
登录 后发表回答