从FragmentTabHost的子片段内部片段添加新的选项菜单

时间:2015-03-20 10:23:47

标签: java android android-layout android-fragments android-activity

当我为我的应用程序使用了一些嵌套片段时,应用程序的子片段无法通过覆盖onCreateOptionsMenu(菜单菜单,MenuInflater menuInflater)方法添加新的选项菜单(从不使用此方法)。

应用程序的类层次结构如下。

MainActivity.java(扩展了SherlockFragmentActivity) (MainActivity包括以下子片段)

  1. Fragment1.java(扩展SherlockFragment)

  2. Fragment2.java(扩展SherlockFragment)

  3. (Fragment2包括FragmentTabHost用于添加以下子片段)

     i. ChildFragment1.java(extends SherlockFragment)
    
     ii. ChildFragment2.java(extends SherlockFragment)
    
     iii. ChildFragment3.java(extends SherlockFragment)
    

    我可以通过在onCreate(Bundle savedInstanceState)中添加setHasOptionsMenu(true)来在Fragment1,Fragment2中添加新的OptionMenu,并且 通过覆盖onCreateOptionsMenu(菜单菜单,MenuInflater menuInflater)

    但是我无法在ChildFragment1,ChildFragment2,ChildFragment3中添加新的OptionMenu,因为onCreateOptionsMenu(菜单菜单,MenuInflater menuInflater)从不调用子片段类的方法。

2 个答案:

答案 0 :(得分:1)

现在我可以调用子片段的onCreateOptionsMenu,也可以通过以下代码调用onOptionsItemSelected。

Fragment2.java

protected FragmentTabHost mTabHost;

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    super.onCreateOptionsMenu(menu, menuInflater);
    Fragment currentFragment = getChildFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
    if (currentFragment != null && currentFragment instanceof BaseTabsPagerChildFragment)
        ((BaseTabsPagerChildFragment) currentFragment).inflateOptionsMenu(menu, menuInflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (super.onOptionsItemSelected(item)) {
        return true;
    } else {
        Fragment currentFragment = getChildFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
        if (currentFragment != null && currentFragment instanceof BaseTabsPagerChildFragment) {
            return ((BaseTabsPagerChildFragment) currentFragment).onOptionsItemSelected(item);
        }
    }
    return false;
}

BaseTabsPagerChildFragment.java

abstract class BaseTabsPagerChildFragment extends BaseSherlockFragment {
    public abstract void inflateOptionsMenu(Menu menu, MenuInflater menuInflater);
}

ChildFragment1.java(扩展BaseTabsPagerChildFragment.java)

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

@Override
public void inflateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    onCreateOptionsMenu(menu, menuInflater);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean handled = super.onOptionsItemSelected(item);
    if (handled) {
        return true;
    }
    int itemId_ = item.getItemId();
    if (itemId_ == R.id.menu_item_compose) {
        launchCompose();
        return true;
    }
    if (itemId_ == R.id.menu_item_delete) {
        doDelete();
        return true;
    }
    return false;
}

感谢。

答案 1 :(得分:0)

您必须手动调用该方法。在您的Fragment2实施方案

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    ChildFragment frag = (ChildFragment) getChildFragmentManager().findFragmentBy...;
    if (frag != null) frag.inflateOptionsMenu(menu, new MenuInflater(mActivity));
}

ChildFragment中,然后

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

public void inflateOptionsMenu(Menu menu, MenuInflater inflater) {
    onCreateOptionsMenu(menu, inflater);
}

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

我在chat后面this question找到了解决方案。