Hide Actionbar On Swipe Vertical Viewpager
I'm trying to hide My ActionBar/toolbar on Swipe using a vertical ViewPager, this is my MainActivity XML: Copy
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"tools:openDrawer="end"tools:ignore="InconsistentLayout"><includelayout="@layout/app_bar_main"android:layout_width="match_parent"android:layout_height="match_parent" /><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="end"android:fitsSystemWindows="true"app:headerLayout="@layout/nav_header_main"app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.sample.MainActivity"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"xmlns:android="http://schemas.android.com/apk/res/android"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"android:layoutDirection="rtl"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay"app:layout_scrollFlags="scroll|enterAlways"/></android.support.design.widget.AppBarLayout><includelayout="@layout/content_main"/></android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.SwipeRefreshLayoutxmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/swiperefreshlayout"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"><android.support.v4.widget.NestedScrollViewandroid:isScrollContainer="false"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"android:clipToPadding="false"tools:context=".MainActivity"android:fillViewport="true"app:layout_behavior="@string/appbar_scrolling_view_behavior"><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:background="@color/colorAccent"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/refreshListBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="10dp"android:background="@drawable/flat_selector"android:textColor="@android:color/white"android:visibility="visible"/><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="Add Your ViewPager Instead of TextView"android:layout_below="@+id/refreshListBtn"android:layout_alignParentTop="true"android:layout_alignParentBottom="true"android:textColor="#FFFFFF"android:gravity="center"/></RelativeLayout></android.support.v4.widget.NestedScrollView></android.support.v4.widget.SwipeRefreshLayout>
Solution 2:
Normally I use this piece of code (see below). But this is prepared for another type of layout. You can try to adapt it to your layout.
publicstaticclassShowHideToolbarOnScrollingListenerimplementsMyNestedScrollView.ScrollViewListener{
private Toolbar toolbar;
private State state;
privatefloat toolbarElevation;
publicShowHideToolbarOnScrollingListener(Toolbar toolbar, float toolbarElevation) {
this.toolbar = toolbar;
this.state = newState();
this.toolbarElevation = toolbarElevation;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)privatevoidtoolbarSetElevation(float elevation) {
if (AndroidUtils.isLollipop()) {
toolbar.setElevation(elevation == 0 ? 1 : toolbarElevation);
}
}
privatevoidtoolbarAnimateShow(finalint verticalOffset) {
toolbar.animate()
.translationY(0)
.setInterpolator(newLinearInterpolator())
.setDuration(180)
.setListener(newAnimatorListenerAdapter() {
@OverridepublicvoidonAnimationStart(Animator animation) {
toolbarSetElevation(verticalOffset == 0 ? 1 : toolbarElevation);
}
});
}
privatevoidtoolbarAnimateHide() {
toolbar.animate()
.translationY(-toolbar.getHeight())
.setInterpolator(newLinearInterpolator())
.setDuration(180)
.setListener(newAnimatorListenerAdapter() {
@OverridepublicvoidonAnimationEnd(Animator animation) {
toolbarSetElevation(1);
}
});
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)publicvoidonRestoreInstanceState(State state) {
this.state.verticalOffset = state.verticalOffset;
this.state.scrollingOffset = state.scrollingOffset;
if (AndroidUtils.isLollipop()) {
toolbar.setElevation(state.elevation);
toolbar.setTranslationY(state.translationY);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)public State onSaveInstanceState() {
state.translationY = toolbar.getTranslationY();
if (AndroidUtils.isLollipop()) {
state.elevation = toolbar.getElevation();
}
return state;
}
@OverridepublicvoidonScrollChanged(MyNestedScrollView v, int x, int y, int oldx, int oldy) {
intdy= y-oldy;
Log.d("dy", ""+dy);
state.verticalOffset = v.computeVerticalScrollOffset();
state.scrollingOffset = dy;
inttoolbarYOffset= (int) (dy - toolbar.getTranslationY());
toolbar.animate().cancel();
if (state.scrollingOffset > 0) {
if (toolbarYOffset < toolbar.getHeight()) {
if (state.verticalOffset > toolbar.getHeight()) {
toolbarSetElevation(toolbarElevation);
}
toolbar.setTranslationY(state.translationY = -toolbarYOffset);
} else {
toolbarSetElevation(1);
toolbar.setTranslationY(state.translationY = -toolbar.getHeight());
}
} elseif (state.scrollingOffset < 0) {
if (toolbarYOffset < 0) {
if (state.verticalOffset <= 0) {
toolbarSetElevation(1);
}
toolbar.setTranslationY(state.translationY = 0);
} else {
if (state.verticalOffset > toolbar.getHeight()) {
toolbarSetElevation(toolbarElevation);
}
toolbar.setTranslationY(state.translationY = -toolbarYOffset);
}
}
}
@OverridepublicvoidonEndScroll() {
if (state.scrollingOffset > 0) {
if (state.verticalOffset > toolbar.getHeight()) {
toolbarAnimateHide();
} else {
toolbarAnimateShow(state.verticalOffset);
}
} elseif (state.scrollingOffset < 0) {
if (toolbar.getTranslationY() < toolbar.getHeight() * -0.6 && state.verticalOffset > toolbar.getHeight()) {
toolbarAnimateHide();
} else {
toolbarAnimateShow(state.verticalOffset);
}
}
}
/**
* Parcelable RecyclerView/Toolbar state for simpler saving/restoring its current state.
*/publicstaticfinalclassStateimplementsParcelable {
publicstatic Creator<State> CREATOR = newCreator<State>() {
public State createFromParcel(Parcel parcel) {
returnnewState(parcel);
}
public State[] newArray(int size) {
returnnewState[size];
}
};
// Keeps track of the overall vertical offset in the listprivateint verticalOffset;
// Determines the scroll UP/DOWN offsetprivateint scrollingOffset;
// Toolbar valuesprivatefloat translationY;
privatefloat elevation;
State() {
}
State(Parcel parcel) {
this.verticalOffset = parcel.readInt();
this.scrollingOffset = parcel.readInt();
this.translationY = parcel.readFloat();
this.elevation = parcel.readFloat();
}
@OverridepublicintdescribeContents() {
return0;
}
@OverridepublicvoidwriteToParcel(Parcel parcel, int flags) {
parcel.writeInt(verticalOffset);
parcel.writeInt(scrollingOffset);
parcel.writeFloat(translationY);
parcel.writeFloat(elevation);
}
}
}
Post a Comment for "Hide Actionbar On Swipe Vertical Viewpager"