我已经改变标签秩序,现在还不能设定动作条福尔摩斯正确 [英] I've changed tabs order, and now can't set actionbar sherlock properly

查看:176
本文介绍了我已经改变标签秩序,现在还不能设定动作条福尔摩斯正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我用actionbarsherlock显示在上侧的上下文菜单,和tabhost显示导航选项底部(每个标签加载一个片段)。

In my project, I use actionbarsherlock to show a context menu at the upper side, and a tabhost to show navigate options at the bottom (each tab loads a fragment).

起初,我的默认选项卡受到一项至极得到用户的GPS位置,并加载地图。第二个选项卡是与几种类型的选项列表。当我决定调换此选项卡(只需先添加类别选项卡TabsAdapter),我开始有问题,改变我的动作条。

At first, my default tab was one wich gets user's gps location and loads a map. The second tab was a list with several categories options. As I decided to swap this tabs (just by adding the categories tab first at the TabsAdapter), I started to have problems changing my actionbar.

在我的应用程序第一次启动时,我可以看到,而不是标题从类别列表片段这个选择类别微调。在MainActivity,我实现ViewPager.OnPageChangeListener修复它,但它只能被调用时,用户更改标签,所以它不是以取下微调时的类别片段首先显示的是

When my app first starts, I can see this "select category" spinner from the "categories list" fragment, instead of the title. At MainActivity, I implement ViewPager.OnPageChangeListener to fix it, but it only gets called when user changes tabs, so its not enough to remove the spinner when the categories fragment is first shown

我已经贴了一些code如下:

I've pasted some code below:

---- MainActivity ------

----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(类别片段)------

-----CategoriasLista (The categories fragment)------

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(地图片段)--------

--------PorPertoFragment (The map fragment)--------

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;

}

我如何能处理这有什么建议?先谢谢了。

Any suggestions on how can I handle this? Thanks in advance.

我想通了,覆盖在prepareOptionsMenu可能是实现这一目标的好方法。它看起来promissing,但我已经在每个片段中做到了,虽然它没有工作固定类片段的第一appearence,这种方法甚至没有被调用时,其他的片段选择......我$ C $下面的CD是这样的:

I figured out overriding onPrepareOptionsMenu could be a good way to achieve that. It looks promissing, but I've done it in each fragment and, while it did the job fixing the categories fragment first appearence, this method doesn't even get called when the other fragments are selected... I've coded it like below:

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

[/编辑]

推荐答案

我只在第一个选项卡的片段overrided在prepareOptionsMenu,也改变到的setTitle在bar.setTitle MainActivity的onPageSelected方法

I've overrided onPrepareOptionsMenu only at the first tab's fragment, and also changed setTitle to bar.setTitle at MainActivity's onPageSelected method

这篇关于我已经改变标签秩序,现在还不能设定动作条福尔摩斯正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆