Roman Panaget
Roman Panaget

Reputation: 2428

Android - Different ActionBar depending on the Fragment in a tabbed Activity

The title says it all. I was wondering if I could modify the action bar items depending on the fragment displayed on a tabbed Activity. If it is possible how should I do this stuff?

Upvotes: 0

Views: 105

Answers (2)

EscapeArtist
EscapeArtist

Reputation: 796

You could use a Toolbar instead of an ActionBar.
http://javatechig.com/android/android-lollipop-toolbar-example
You can make as many Toolbars as you need, and show and hide them on tabHost.setOnTabChangedListener(...) the way Сергей Боиштян described.
I find Toolbars easier to customize and less messy than Actionbars

Upvotes: 1

you can do this.

public class UserTabsActivity extends BaseActivity implements TabHost.OnTabChangeListener {

private MenuItem                       settingsItem;
private MenuItem                       actionSearch;
private SearchView                     searchView;
private SearchView.OnQueryTextListener onQueryTextListener;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.user_tabs_menu, menu);
    settingsItem = menu.findItem(R.id.settings_item);
    settingsItem.setOnMenuItemClickListener(this);

    actionSearch = menu.findItem(R.id.action_search);
    searchView = (SearchView) actionSearch.getActionView();
    searchView.setQueryHint(Constants.USER_HINT);
    searchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
    searchView.setOnQueryTextListener(onQueryTextListener);
    return true;
}
@Override
public void onTabChanged(String tabId) {
    customize(tabId)
}
public void customize(String currentTab) {
    switch (currentTab) {
        case "sometag":
            settingsItem.setVisible(false);
            actionSearch.setVisible(true);
            break;
        default:
            settingsItem.setVisible(true);
            actionSearch.setVisible(false);
    }
}

And in you fragment yo can write some logic

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    FragmentTabHost tabHost = new FragmentTabHost(getActivity());
    tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.content);

    tabHost.addTab(tabHost.newTabSpec("users").setIndicator("ПОЛЬЗОВАТЕЛИ"), SomeFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec("posts").setIndicator("ПОСТЫ"), SomeFragment.class, null);

    tabHost.setOnTabChangedListener((TabHost.OnTabChangeListener) getActivity());

    return tabHost;
}

In that code is the most important tabHost.setOnTabChangedListener(..) and UserTabsActivity extends BaseActivity implements TabHost.OnTabChangeListener This is one of the possibilities. I hoped, this is helpfull

Upvotes: 0

Related Questions