Search code examples
javaandroidandroid-fragmentsactionbarsherlockandroid-menu

Custom action bar for each fragment - not menu


Let me explain first: my application is planed to use 3 activities with swipe navigation. I am using different sherlock action bar for each one of them.

But since it is not possible to swipe between activities, the only solution is to work with fragments. I implemented that, but I came across a problem: is it possible to customize action bar for each fragment? Because now I'm stuck only with one. I've Googled and every solution is based on a menu, but is it possible to apply custom styles to menus and in fragments?

Sorry if this was already answered, I haven't found any solution online.

Is it possible to use this kind of approach?

public class ControlActivity extends SherlockFragmentActivity {

ActionBar actionBar;
View actionBarView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout);
    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    actionBar = getSupportActionBar();
    actionBarView= getLayoutInflater().inflate(
            R.layout.friend_list_ab_layout, null);
    actionBar.setCustomView(actionBarView);


}
private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public SherlockFragment getItem(int pos) {
        switch(pos) {

            case 0:
                actionBarView= getLayoutInflater().inflate(
                        R.layout.friend_list_ab_layout, null);
                actionBar.setCustomView(actionBarView);
                return MainActivity.newInstance();
            case 1:
                actionBarView= getLayoutInflater().inflate(
                        R.layout.news_feed_ab_layout, null);
                actionBar.setCustomView(actionBarView);
                return NewsFeed.newInstance();
            default:
                actionBarView= getLayoutInflater().inflate(
                        R.layout.friend_list_ab_layout, null);
                actionBar.setCustomView(actionBarView);
                return MainActivity.newInstance();
        }
    }
    @Override
    public int getCount() {
        return 2;
    }
}

}


Solution

  • well you can build your own action bar, what i mean to say is that you should create a dynamic fragment and use it at an action bar and give it styling a as action bar, use frame layout as a plcaeholder for the fragment and when you swipe the fragment replace the actionbar fragment with new one