-2

I am using ViewPager in my app. In each fragment there is a toolbar. On a single tap on the image the the toolbar is animated to the top out of the screen. But I have to notify all the remaining fragments to do the same thing. So that when the user scrolls to the next fragment he doesn't see the toolbar.

I tried adding setUserVisibleHint(), but it did not work as it was called only when the fragment was completely visible, thus showing the toolbar exiting to the user.

Then I tried it in onResume and setting pager.offscreenpagelimit=1, it worked fine for the fragment next to next but did not work for the next fragment.

Thanks!!

Darth Hunterix
  • 1,484
  • 5
  • 27
  • 31
umesh lohani
  • 162
  • 1
  • 4
  • 13

2 Answers2

0

First Notify your activity from fragment using:

In Fragment on Animation End:

((YourActivity)getActivity()).hideToolbar(); 

In Activity:

public void hideToolbar() {
// Redraw view pager without toolbar (notify your adapter create pager without toolbar)
}

Well, why don't you get your toolbar out of your fragments and just create one in activity and change its state on page change (definitely it will not slide but may

Chirag Jain
  • 1,612
  • 13
  • 20
  • I have to add a toolbar to every fragment in the viewpager so that when I scroll each page I see the a different toolbar with every page. – umesh lohani Mar 09 '15 at 12:19
0

You have 2 problems to face.

  1. How to call existing fragments to hide/show their toolbars.
  2. How to create another fragments with hidden toolbar.

First problem can be easly done by using Otto event library found here. Just paste this code in your viewpager fragments:

Bus bus = new Bus();

@Override
protected void onResume() {
    super.onResume();
    bus.register(this);
}

@Override
protected void onPause() {
    super.onPause();
    bus.unregister(this);
}

@Subscribe
public void onToolbarEvent(ToolbarEvent event) {
    //do your toolbar logic
}

Then in your onClick event on image just put (of course creating bus object before)

bus.post(new ToolbarEvent());

ToolbarEvent can be just empty class. If you read about Otto events you will understand how it works.

Another problem is how to know that the toolbar should be hidden/shown, when viewpager instantiates new fragments? Simply add a boolean flag in your shared prefferences, so every time fragment is created, it can check if it can show toolbar or not e.g. in onViewCreated() method. The example how to use shared prefferences can be found here

Hope I helped a little bit.

rwojcik
  • 1,030
  • 9
  • 18