1

Currently I have action bars for each fragment, when I swipe the fragment a new actionbar appears in place of the old one. What I want to do is when I swipe, I want to swipe away the actionbar and place it with a new one. Each fragment should have their own actionbar.

I'm not sure if its making complete sense so I drew a quick demonstration of what I'm trying to do.

Action Bar Demonstration

Errol Green
  • 1,367
  • 5
  • 19
  • 32

3 Answers3

0

In your viewpager container (Activity or Fragment), add the next line in your page change listener:

invalidateOptionsMenu();
supportInvalidateOptionsMenu();//if using the actionbar support library
getActivity().invalidateOptionsMenu();//if your viewpager container is a fragment

Then in your onCreateOptionsMenu(),

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.your_menu_layout, menu);
}

The onPrepareOptionsMenu() callback method is called before the menu is shown, and we are going to use it to make the menu items visible depending on the current fragment:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    int page = yourViewPager.getCurrentItem();
    switch(page) {
        case 0:
            menu.findItem(R.id.item_f1).setVisible(true);
            menu.findItem(R.id.item_f2).setVisible(false);
            menu.findItem(R.id.item_f3).setVisible(false);
            break;
        case 1:
            menu.findItem(R.id.item_f1).setVisible(false);
            menu.findItem(R.id.item_f2).setVisible(true);
            menu.findItem(R.id.item_f3).setVisible(false);
            break;
        case 2:
            menu.findItem(R.id.item_f1).setVisible(false);
            menu.findItem(R.id.item_f2).setVisible(false);
            menu.findItem(R.id.item_f3).setVisible(true);
            break;  
    }
    return true;

If your viewpager container is a fragment, add setHasOptionsMenu(true), if it is an activity it's not necessary.

Reference - Swipe view with different actionbar items in each swipe fragment

Community
  • 1
  • 1
VikramV
  • 1,111
  • 2
  • 13
  • 31
  • This is what I currently have roughly, What this does is just replace the existing actionbar with different items. When I swipe the page, I want the actionbar to slide with the fragment onto the new page. As demonstrated in my picture. – Errol Green Jul 11 '15 at 08:56
0

You could be using the Toolbar from the AppCompat library. Add a view to this Toolbar, which you move based on the position of your ViewPager. For making such a view, check this question.

You could also try multiple Toolbars, which might be possible, but it is not supported. Which could result in breaking your app, if the AppCompat library does get updated. Although it looks like it is possible, according to this question.

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Yup that would do it, However toolbar uses API 21 and up which would limit a good chunk of devices. It would be preferred if it can be done with ActionBar – Errol Green Jul 11 '15 at 08:58
  • You should use the Toolbar from the AppCompat library, which is compatible back to API level 7. Check this blog post for more info: http://android-developers.blogspot.hu/2014/10/appcompat-v21-material-design-for-pre.html Especially the section 'Toolbar widget'. – Daniel Zolnai Jul 11 '15 at 09:08
0

I don't think this can be done with ActionBar, Its just too limited... I've just went ahead and made my own custom layout that I use on my fragments.

Since i'm implementing a lot of features into my actionbar I'm probably best of customizing my own.

Errol Green
  • 1,367
  • 5
  • 19
  • 32