I've changed tabs order, and now can't set actionbar sherlock properly

时间秒杀一切 提交于 2019-12-11 20:11:44

问题


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).

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.

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

I've pasted some code below:

----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 (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 (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.

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

[/EDIT]


回答1:


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



来源:https://stackoverflow.com/questions/19451385/ive-changed-tabs-order-and-now-cant-set-actionbar-sherlock-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!