Swipe view with different actionbars - android

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.

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

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.

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.

Related

ActionBarCompat + tabs: remove Fragment Menu Item when switching?

For each Fragment I will add Menu Items to provide the user with context related actions. But when switching to another Fragment, the Menu items stayed. So I can end with all options from each fragment in the menu.
How do you clean the menu item to have only the ones set up in the activity menu?
I read about invalidateOptionMenu() and onPrepareOPtionmenu() but I don't really get how they work. What is the correct way to implement it ?
Anyway, how is it that removing fragment-linked menu item when the fragment is not displayed anymore is not native Android behavior?
EDIT if instrucitons not clear enough.
I have one activity supporting Navigation_Mode_Tabs with settings as global menu item.
I start fragA that add itemA1 and itemA2. So I have setting, itemA1, itemA2. So far, so good.
Then I switch to fragB that add its own itemB. HERE, I have settings, itemA1, itemA2, itemB in the menu !
Why itemA1 and itemA2 are stuck? How to remove them?
EDIT 2 : I have try another set up of tabulation from this post and it suddenly menu items seems to be correctly remove !
It's something to see with the use of remove(R.id.container, fragment) versus the onDetach()/onAttach() that Google recommands here
I am looking at this, probably tomorrow I'll update. Please, if you know about this, share :)
you could set the menuinflater on each activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.demo, menu);
return true;
}
You should add TabListener to your tabs and call supportInvalidateOptionsMenu() in onTabSelected() method. It cause call onCreateOptionsMenu(). You should override it in your activity and inflate menu for selected fragment. If you add menu items from code don't forget to clear menu for previous fragment.

Altering ActionBar from Fragments

I need to make two minor changes to an Activity's ActionBar from a Fragment.
The parent FragmentActivity's onCreateOptionMenu and onPrepareOptions handle the menu across the app.
However, in the containing main Fragment view, I want the ActionBar title to change for the sake of navigation. Each time the Fragment changes.
Also, in one instance, I want to add a single menu item as defined in the layout. I rather not duplicate code for this part, do I have to override the same methods and set up entire menu again, including onOptionSelected? Or...?
Edit:
On my menu xml layout, I have this item:
<item
android:id="#+id/Add"
android:title="Add Items"/>
This item needs to be seen in the ActionBar only for one particular Fragment. The rest of the menu is identical to what should be seen for the rest of the app. In the fragment where I need this new item, do I have to also do onCreateOptionsMenu, onPrepareOptionsMenu, and onOptionsItemSelected all over again and duplicate thee OTHER menu items as well? Or can I override those in the Fragment and just pop that one extra menu item in?
1.
getActivity().getActionBar().setTitle();
or
getActivity.getSupportActionBar().setTitle();
more clarification please :)
This answered what I needed for part 2.
Putting this in onActivityCreated:
setHasOptionsMenu(true);
And then adding this too:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
ActivityCompat.invalidateOptionsMenu(getActivity());
MenuItem Add = menu.findItem(R.id.Add);
Add.setVisible(true);
}
The parent Activity sets the R.id.Add as invisible by default.

Android: How to recreate Action bar when fragment changed

I have an activity showing a few fragments.
Activity view contains only ViewPager initialized with custom FragmentPagerAdapter.
This adapter provide navigation among 3 fragments.
All seems to work fine except Action bar.
I override onCreateOptionsMenu() method in my fragments to create individual Action bar for any fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
//fragment specific menu creation
}
When I do swipe, new fragment appears, but Action bar stay the same for a few seconds. Probably after a few seconds this method are called and Action bar get changed.
This looks pretty bad when action bar are changed in a while after swipe is finished.
How can I recreate action bar immediately, before swipe begin?
You can ask android to re-create the actionbar before it automatically does by calling invalidateOptionsMenu();
Do this somewhere close to the point where you change fragments to try and decrease the 'lag' between the fragment and actionbar changing.
Edit
a complete solution may look like this:
class activity extends Activity{
private void switchFragment(){
...do fragment changing stuff
activity.invalidateOptionsMenu();
}
}
class someFragment extends Fragment{
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
//fragment specific menu creation
}
}
whichever fragment is open during the
activity.invalidateOptionsMenu();
will then call its
onCreateOptionsMenu
and you can do fragment specific menu creation in there
It can be done by implementing onCreateOptionsMenu and calling setHasOptionsMenu(true) in the onAttach callback in each fragment and change the actions accordingly.
To show the fragment without any actions setHasOptionsMenu(false) in it
If you are using ActionBar tabs, You would like to see this answer:
How can I change Action Bar actions dynamically?
Finally I couldn't find any sound way to achieve this.
The best would be to make actionbar a part of fragment, not activity, but it's impossible.
I end up with clearing actionbar menu and title when swipe begins(you can set special listener in PageView) and setting menu and title again when swipe complete and new fragment are shown.
In gives some time lag and actionbar looks strangely empty during swipe, but it's best you can do.
Android API is c...

ActionBar Menu Items disappear in NestedFragments

Since android 4.2 now support NestedFragment , and added it to support v13.
I use this NestedFragment on a classic situation : Create fragmentA that can swipe left and right and consume a majority of the screen space, and insert fragmentB and fragmentC into each fragment page.
My Problem is the MenuItem I create in fragmentB and fragmentC can`t show on Activity`s actionbar.Which before I use NestedFragment , it works well.
got at some point the same problem. If you're using the ActionBarSherlock library this is a small bug. What you basically have to do is to call from your parent fragment from the onCreateOptionsMenu() method the onCreateOptionsMenu() method of the children, something like:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getChildFragment().onCreateOptionsMenu(menu, inflater);
}
hope this works, let me know.
Cheers.

Fragment Specific Actionbar MenuItems

I'm building an application with ActionBarSherlock that uses the Dropdown list navigation style. I have it set that each dropdown list item loads a different fragment, and that works fine. What doesn't work is the menu items in the actionbar. I have setHasOptionsMenu(true) in the fragments that I want to have menu items, as well as
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
for the menus in the fragments. Every time I change fragments, I don't want the menu items appended which is what's happening. When one fragment is selected, the menu loads fine, then a different fragment is selected that isn't supposed to have menu items, and the menu items are the same as the previous fragment. Then if I go back to the first fragment, the menu items get doubled because they keep getting appended. How can I control this?
In the normal case the menu shouldn't be appended. What does your menu.xml look like? Do you have id's set? Maybe creating a menu in the Activity?
I figured it out. I wasn't using FragmentTransaction to load the fragments.

Categories

Resources