0

我試圖隱藏使用垂直ViewPager上刷卡我動作條/工具欄,這是我的MainActivity XML:隱藏動作條

<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> 

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

+1

添加此的工具欄應用程序:layout_scrollFlags =「滾動| enterAlways」 – MinnuKaAnae

+0

謝謝你,現在我後滾動動作條隱藏,但仍然黑屏,我會添加我的片段代碼。 –

+0

一旦從按鈕中刪除android:visibility =「gone」,並檢查或添加一些背景顏色以檢查其是否正常工作 – MinnuKaAnae

回答

0

問題與SwipeRefreshLayout,這應該是對NestedScrollView佈局的頂部。

<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> 
+0

它不工作仍然是空的片段,就像視頻在我的問題 –

+0

問題不在'SwipeRefreshLayout'我完全刪除它,而且仍然是相同的 –

+0

我已經發布樣本演示https://www.youtube.com/watch?v=QC-lzyAy8U8 –

0

通常我使用這段代碼(見下文)。但是這是爲另一種佈局做準備的。您可以嘗試使其適應您的佈局。

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); 
     } 
    } 
}