i've managed to 'bypass' the problem.
I've created an abstract class, use it in your projects !
THIS IS LAYOUT:
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/status_bar_height" />
<FrameLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_app_bar"
android:background="@color/appbar"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="@dimen/status_bar_height"
android:background="@color/appbar" />
<android.support.design.widget.AppBarLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:background="@color/appbar"
/>
</FrameLayout>
</FrameLayout>
The framelayout'll be your "new" appbar.
Then, your fragments (the ones in the viewpager) must extend this class:
public abstract class SnappableAppBarFragment extends Fragment {
public int scrollAttuale;
private boolean attivaSnap = true;
private boolean isTouching = false;
public void setSnapActivated(boolean state){attivaSnap = state;}
public void setUpSnappableAppbar(final View fragMainView, final NestedScrollView nestedScrollView, final FrameLayout appBar, final int actionBarHeight) {
nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (!attivaSnap)
return;
int scrollY = nestedScrollView.getScrollY();
int differenza = scrollAttuale - scrollY;
if (differenza > 0 && appBar.getY() < 0) {
//Esci
appBar.animate().translationYBy(differenza).setDuration(0).start();
if (appBar.getY() > 0) {
appBar.animate().translationY(0).setDuration(0).start();
}
}
if (differenza < 0 && appBar.getY() > -actionBarHeight) {
//Entra
appBar.animate().translationYBy(differenza).setDuration(0).start();
if (appBar.getY() < -actionBarHeight)
appBar.animate().translationY(-actionBarHeight).setDuration(0).start();
}
if (differenza >= -2 && differenza <= 2 && !isTouching ){
int spazioTot = actionBarHeight;
if ((Math.abs(appBar.getY()) < spazioTot / 2 || nestedScrollView.getScrollY() <= 200) && appBar.getY() != 0) {
//Espandi
appBar.animate().translationY(0).setDuration(270).start();
} else if (appBar.getY() != 0) {
//Chiudi
appBar.animate().translationY(-actionBarHeight).setDuration(270).start();
}
}
scrollAttuale = scrollY;
//Scrolling verso l'alto differenza è positiva
//Scrolling verso il basso differenza è negativa
}
});
fragMainView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!attivaSnap)
return false;
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
isTouching = false;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
int spazioTot = actionBarHeight;
// && nestedScrollView.getScrollY() <= 200 && nestedScrollView.getScrollY() <= 200 && nestedScrollView.getScrollY() <= 200
if ((Math.abs(appBar.getY()) < spazioTot / 2 || nestedScrollView.getScrollY() <= 200) && appBar.getY() != 0) {
//Espandi
appBar.animate().translationY(0).setDuration(270).start();
} else if (appBar.getY() != 0) {
//Chiudi
appBar.animate().translationY(-actionBarHeight).setDuration(270).start();
}
}
}, 0);
}
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
isTouching = true;
}
return false;
}
});
}
}
And finally, in fragments:
public class Fragment1 extends SnappableAppBarFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
viewPrincipale = inflater.inflate(R.layout.fragment_home, container, false);
mainActivity = (MainActivity) getActivity();
setUpSnappableAppbar(viewPrincipale, (NestedScrollView) viewPrincipale.findViewById(R.id.nestedScrollView), parentAppBar, actionBarHeight);
...
}
public void setParentAppBar(FrameLayout frameLayout) {
parentAppBar = frameLayout;
}
public void setActionBarHeight(int height) {
actionBarHeight = height;
}
...
}
Eventually, when declaring fragments, set parentappbar and actionbarheight from activity:
fragment1.setParentAppBar((FrameLayout) findViewById(R.id.appBarLayout));
fragment1.setActionBarHeight(toolbar.getLayoutParams().height);
That's it, sorry if it's long but is the only way i found in order to make it work !
Also, sorry for bad english, i'm an italian developer :P
Bye
EDIT: IMPORTANT!! CHANGE IN SNAPPABLEAPPBARFRAGMENT THIS: final int actionBarHeight TO final float actionBarHeight !!! IT'LL BE SMOOTHER :D