android - 每个带有 ActionBarSherlock 的选项卡 fragment 的操作栏菜单

标签 android tabs actionbarsherlock fragment

我在使用 ActionBarSherlock API 为我的 Activity 的每个 fragment 设置选项菜单时遇到了一些问题。

我已将 setHasOptionsMenu(true) 放置在我的 onCreateView 中,但从未调用 fragment 中的 onCreateOptionsMenu

我的主要 Activity

public class EvolutionActivity extends TabActivity {

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

    @Override
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();    
        addTab(getResources().getString(R.string.fragment_measurement_title), TabFragmentMeasurement.class, null);
        addTab(getResources().getString(R.string.fragment_workout_title) , TabFragmentWorkout.class, null);
        addTab(getResources().getString(R.string.fragment_exercise_title), TabFragmentExercise.class, null);
    }

}

Tab Activity 抽象类 TabActivity.class

public abstract class TabActivity extends SherlockFragmentActivity {

private ViewPager mViewPager;
private TabsAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    /*
     * Create the ViewPager and our custom adapter
     */
    mViewPager = new ViewPager(this);
    adapter = new TabsAdapter( this, mViewPager );
    mViewPager.setAdapter( adapter );
    mViewPager.setOnPageChangeListener( adapter );

    /*
     * We need to provide an ID for the ViewPager, otherwise we will get an exception like:
     *
     * java.lang.IllegalArgumentException: No view found for id 0xffffffff for fragment TabFragmentMeasurement{40de5b90 #0 id=0xffffffff android:switcher:-1:0}
     * at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:864)
     *
     * The ID 0x7F04FFF0 is large enough to probably never be used for anything else
     */
    mViewPager.setId( 0x7F04FFF0 );

    super.onCreate(savedInstanceState);

    /*
     * Set the ViewPager as the content view
     */
    setContentView(mViewPager);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.action_settings:{
        Intent intent = new Intent(this, ParametersActivity.class);
        startActivity(intent);
        break;
    }
    }
    return super.onOptionsItemSelected(item);
}

/**
 * Add a tab with a backing Fragment to the action bar
 * @param titleRes A string resource pointing to the title for the tab
 * @param fragmentClass The class of the Fragment to instantiate for this tab
 * @param args An optional Bundle to pass along to the Fragment (may be null)
 */
protected void addTab(int titleRes, Class fragmentClass, Bundle args ) {
    adapter.addTab( getString( titleRes ), fragmentClass, args );
}
/**
 * Add a tab with a backing Fragment to the action bar
 * @param titleRes A string to be used as the title for the tab
 * @param fragmentClass The class of the Fragment to instantiate for this tab
 * @param args An optional Bundle to pass along to the Fragment (may be null)
 */
protected void addTab(CharSequence title, Class fragmentClass, Bundle args ) {
    adapter.addTab( title, fragmentClass, args );
}

我的 fragment TabFragmentMeasurement.class

public class TabFragmentMeasurement extends Fragment {

....... // Class' attributes

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        setHasOptionsMenu(true);

        TextView txt = new TextView( inflater.getContext() );

        ....... // Change TextView's attributes

        return txt;
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {

    case R.id.item_add_measurement:{

               // Add a measurement

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

XML 菜单文件 measurement.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/item_add_measurement"
    android:icon="@android:drawable/ic_menu_add"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="@string/item_add_measurement"/>

</menu>

最佳答案

为什么不在 fragment 类中扩展 SherlockFragment? 请记住,您传递的每个上下文(如“this”)都应后跟:

this.getSherlockActivity().

关于android - 每个带有 ActionBarSherlock 的选项卡 fragment 的操作栏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18551194/

相关文章:

java - 动画添加到舞台

android - 翻译动画部分的屏幕变为空白

python - QLayout:尝试将 QLayout ""添加到 QWidget "",它已经有一个布局

android - 如果选项卡位置发生更改或通过调用 onPageChangeListener 关闭搜索 View

android - 操作栏中的后退箭头 Sherlock 未显示

Android - Unity3D 在某些手机上的启动画面中卡住

android - 为什么我必须分离 <intent-filter> 才能在 google play 上显示 "open"按钮?

java - ActionBar 中的选项卡数量

jQuery Tabs,如何防止 anchor 跳转

android - 实现操作栏 : ABSherlock or ABCompat?