在我的项目中,我使用 actionbarsherlock 在上方显示上下文菜单,并使用 tabhost 在底部显示导航选项(每个选项卡加载一个片段)。
起初,我的默认选项卡是获取用户 GPS 位置并加载地图的选项卡。第二个选项卡是一个包含多个类别选项的列表。当我决定交换这个选项卡时(只需首先在 TabsAdapter 中添加类别选项卡),我开始在更改我的操作栏时遇到问题。
当我的应用程序第一次启动时,我可以从“类别列表”片段中看到这个“选择类别”微调器,而不是标题。在 MainActivity,我实现了 ViewPager.OnPageChangeListener 来修复它,但它只在用户更改选项卡时被调用,因此在第一次显示类别片段时移除微调器是不够的
我在下面粘贴了一些代码:
- - 主要活动 - - -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("");
setContentView(R.layout.activity_main);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOnPageChangeListener(this);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec("Por Perto").setIndicator(prepareTabView("Categorias",R.drawable.maisapp)), CategoriasLista.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Categorias").setIndicator(prepareTabView("Por Perto",R.drawable.por_perto)), PorPertoFragment.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Baixados").setIndicator(prepareTabView("Baixados",R.drawable.cupons)), BaixadosFragment.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Sobre").setIndicator(prepareTabView("Configurações",R.drawable.setup)), ConfiguracoesFragment.class, null);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
//This method gets called whenever user changes tab
public void onPageSelected(int position) {
supportInvalidateOptionsMenu();
if (position != 1) {
com.actionbarsherlock.app.ActionBar bar = getSupportActionBar();
bar.setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_STANDARD);
//bar.setDisplayShowTitleEnabled(false);
switch (position){ //Seta a title da action bar conforme a viewpager TODO nao funcionou
case 0:
setTitle("Categorias");
break;
case 2:
setTitle("Baixados");
break;
case 3:
setTitle("Configurações");
break;
}
} else {
com.actionbarsherlock.app.ActionBar bar = getSupportActionBar();
bar.setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);
setTitle("");
}
}
-----CategoriasLista(类别片段)-----
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.categorias_lista, null);
setHasOptionsMenu(true);
adapter = new CategoriasListaAdapter(getActivity().getApplicationContext());
ListView list = (ListView)v.findViewById(R.id.catList);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return v;
}
--------PorPertoFragment(地图片段)--------
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.por_perto_fragment, null);
buscarButton = (Button) v.findViewById(R.id.buscar);
loadingLayout = (RelativeLayout) v.findViewById(R.id.loadingLayout);
ActionBar bar = getSherlockActivity().getSupportActionBar();
getSherlockActivity().supportInvalidateOptionsMenu();
ArrayAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.action_list,
R.layout.sherlock_spinner_item);
mSpinnerAdapter.setDropDownViewResource(R.layout.spinner_item);
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() {
// Get the samfre strings provided for the drop-down's ArrayAdapter
String[] strings = getResources().getStringArray(R.array.action_list);
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
selectedCategory = position;
if (mNaviFirstHit) {
mNaviFirstHit = false;
return true;
}
//IMPORTANT: avoids Nullexception.
if (mMap != null)
downloadJson();
return true;
}
};
bar.setNavigationMode(bar.NAVIGATION_MODE_LIST);
bar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
bar.setSelectedNavigationItem(selectedCategory);
return v;
}
关于如何处理这个问题的任何建议?提前致谢。
[编辑]我发现覆盖 onPrepareOptionsMenu 可能是实现这一目标的好方法。它看起来很有希望,但我已经在每个片段中完成了它,虽然它完成了修复类别片段首次出现的工作,但在选择其他片段时甚至不会调用此方法......我已经将它编码为以下:
@Override
public void onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
Log.e("CategoriasLista", "Executou onPrepareOptionsMenu");
ActionBar bar = getSherlockActivity().getSupportActionBar();
getSherlockActivity().supportInvalidateOptionsMenu();
bar.setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_STANDARD);
bar.setTitle("Categorias");
}
[/编辑]