简体   繁体   English

每个带有ActionBarSherlock的选项卡片段的操作栏菜单

[英]Action bar menu for each tab fragment with ActionBarSherlock

I am having some trouble getting an options menu working for each fragments of my activity using ActionBarSherlock API. 我无法使用ActionBarSherlock API为我的活动的每个片段使用选项菜单。

I have placed setHasOptionsMenu(true) inside my onCreateView , but onCreateOptionsMenu inside fragments is never called. 我已经把setHasOptionsMenu(true)我里面onCreateView ,但onCreateOptionsMenu内片段不会被调用。

My Main 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 abstract class TabActivity.class Tab活动抽象类 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 );
}

My fragment TabFragmentMeasurement.class 我的片段 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 menu file measurement.xml 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>

Why don't you extends SherlockFragment in the fragment classes?? 为什么不在fragment类中扩展SherlockFragment Keep in mind that every context you pass like "this" should be followed by : 请记住,您传递的每个上下文(例如“ this”)都应跟随:

this.getSherlockActivity().

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

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