如果您使用选项卡或视图分页,可以使用onPrepareOptionsMen,它可以让您根据所在的选项卡更改按钮(只需使用switch case):
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
switch ((int) _viewPager.getCurrentItem()) {
case 0:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
case 1:
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
break;
case 2:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
}
return super.onPrepareOptionsMenu(menu);
}
或者只是一个简单的菜单。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
你只需要将项目放入菜单XML文件中。
*添加选项卡:*
public class MainActivity extends SherlockFragmentActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ViewPager _viewPager;
private ActionBar _actionBar;
private Tab _Tab;
private TabsAdapter _tabAdapter;
private int _viewPagerOffScreenLimit = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_viewPager = (ViewPager) findViewById(R.id.viewpager);
_viewPager.setOffscreenPageLimit(_viewPagerOffScreenLimit);
_tabAdapter = new TabsAdapter(this, _viewPager);
_actionBar = getSupportActionBar();
_actionBar.setTitle(getResources().getString(R.string.app_name));
_actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
_actionBar.setHomeButtonEnabled(true);
_tabAdapter.addTab(_actionBar.newTab().setCustomView(getTabIndicator(getString(R.string.tab), R.drawable.ic_launcher)), FragmentTab.class, null);
}
private View getTabIndicator(String text, int drawable) {
View indicator = _inflater.inflate(R.layout.tabs, null);
((TextView) indicator.findViewById(R.id.tab_title)).setText(text);
((ImageView) indicator.findViewById(R.id.tab_icon)).setImageResource(drawable);
return indicator;
}
public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
}
}