我已经更改了标签顺序,现在无法正确设置actionbar sherlock

时间:2013-10-18 13:50:57

标签: android android-actionbar actionbarsherlock android-viewpager android-tabhost

在我的项目中,我使用actionbarsherlock在上方显示上下文菜单,并使用tabhost在底部显示导航选项(每个选项卡加载一个片段)。

首先,我的默认标签是获取用户的gps位置并加载地图的标签。第二个选项卡是一个包含多个类别选项的列表。当我决定交换这个标签时(只需在TabsAdapter中添加类别标签),我就开始在更改操作栏时遇到问题。

当我的应用程序首次启动时,我可以从“类别列表”片段中看到这个“选择类别”微调器,而不是标题。在MainActivity中,我实现了ViewPager.OnPageChangeListener来修复它,但它只在用户更改选项卡时被调用,因此在首次显示类别片段时它不足以删除微调器

我在下面粘贴了一些代码:

---- MainActivity ------

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");
}

[/编辑]

1 个答案:

答案 0 :(得分:0)

我只在第一个标签的片段上覆盖了PrepareOptionsMenu,并且还在MainActivity的onPageSelected方法中将setTitle更改为bar.setTitle