隐藏滑动垂直视图Pager上的操作栏



我正试图使用垂直ViewPager隐藏Swipe上的ActionBar/工具栏,这是我的MainActivityXML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="InconsistentLayout">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<include
android:id="@+id/toolbar"
layout="@layout/view_toolbar" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/view_home" />
</android.support.design.widget.CoordinatorLayout>
<include layout="@layout/view_left_drawer" />
</android.support.v4.widget.DrawerLayout>

我在工具栏中添加了:app:layout_scrollFlags="scroll|enterAlways"

我的片段中只有空白页,ViewPager不可见:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:isScrollContainer="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:background="@color/greeen_theme"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/light_theme_color_read"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/refreshListBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_refresh_list_button_selector"
android:textColor="@android:color/white"
android:visibility="visible"/>
<com.axample.android.view.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.axample.android.adapter.vertical.VerticalViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/pager_padd_botom"
android:clipToPadding="false"/>
</com.axample.android.view.SwipeRefreshLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

视频:https://www.youtube.com/watch?v=sWknAZFs6RA

请帮忙!!

更新:我的view_left_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/black_main"
tools:showIn="@layout/activity_home">
<include layout="@layout/view_drawer_content"/>
</FrameLayout>

我的SwipeRefreshLayout:https://github.com/FredJul/Flym/blob/master/Flym/src/main/java/net/fred/feedex/view/SwipeRefreshLayout.java

SwipeRefreshLayout的问题,它应该在NestedScrollView Layout的顶部。

<android.support.v4.widget.SwipeRefreshLayout
<android.support.v4.widget.NestedScrollView
</<android.support.v4.widget.NestedScrollView>
</<android.support.v4.widget.SwipeRefreshLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns: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">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android: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.CoordinatorLayout
android: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.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:layoutDirection="rtl">
<android.support.v7.widget.Toolbar
android: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>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns: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.NestedScrollView
android: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">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android: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"/>
<TextView
android: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>

通常我使用这段代码(见下文)。但这是为另一种类型的布局做准备的。你可以试着使它适应你的布局。

public static class ShowHideToolbarOnScrollingListener implements MyNestedScrollView.ScrollViewListener{
private Toolbar toolbar;
private State state;
private float toolbarElevation;
public ShowHideToolbarOnScrollingListener(Toolbar toolbar, float toolbarElevation) {
this.toolbar = toolbar;
this.state = new State();
this.toolbarElevation = toolbarElevation;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void toolbarSetElevation(float elevation) {
if (AndroidUtils.isLollipop()) {
toolbar.setElevation(elevation == 0 ? 1 : toolbarElevation);
}
}
private void toolbarAnimateShow(final int verticalOffset) {
toolbar.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
toolbarSetElevation(verticalOffset == 0 ? 1 : toolbarElevation);
}
});
}
private void toolbarAnimateHide() {
toolbar.animate()
.translationY(-toolbar.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
toolbarSetElevation(1);
}
});
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onRestoreInstanceState(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;
}

@Override
public void onScrollChanged(MyNestedScrollView v, int x, int y, int oldx, int oldy) {
int dy = y-oldy;
Log.d("dy", ""+dy);
state.verticalOffset = v.computeVerticalScrollOffset();
state.scrollingOffset = dy;
int toolbarYOffset = (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());
}
} else if (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);
}
}
}
@Override
public void onEndScroll() {
if (state.scrollingOffset > 0) {
if (state.verticalOffset > toolbar.getHeight()) {
toolbarAnimateHide();
} else {
toolbarAnimateShow(state.verticalOffset);
}
} else if (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.
*/
public static final class State implements Parcelable {
public static Creator<State> CREATOR = new Creator<State>() {
public State createFromParcel(Parcel parcel) {
return new State(parcel);
}
public State[] newArray(int size) {
return new State[size];
}
};
// Keeps track of the overall vertical offset in the list
private int verticalOffset;
// Determines the scroll UP/DOWN offset
private int scrollingOffset;
// Toolbar values
private float translationY;
private float elevation;
State() {
}
State(Parcel parcel) {
this.verticalOffset = parcel.readInt();
this.scrollingOffset = parcel.readInt();
this.translationY = parcel.readFloat();
this.elevation = parcel.readFloat();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(verticalOffset);
parcel.writeInt(scrollingOffset);
parcel.writeFloat(translationY);
parcel.writeFloat(elevation);
}
}
}

最新更新