2

我有一個選項卡式活動。有兩個選項卡。標籤1和標籤2之間的唯一區別是,標籤1具有浮動動作按鈕,標籤2沒有。使用浮動操作按鈕,一起滑動刷新佈局和協調器佈局和工具欄佈局行爲

在表1:

  • 當用戶向下滾動再循環器視圖,工具欄和浮動動作按鈕應消失。
  • 當用戶向上滾動回收站視圖時,應該出現工具欄和浮動操作按鈕。

在表2:

  • 當用戶向下滾動再循環器視圖,工具欄應消失。
  • 當用戶向上滾動回收站視圖時,應該出現工具欄。

標籤2很好,在標籤2上沒有問題,但標籤1無法正常工作。當向下滾動回收站視圖時,工具欄不會消失。當滾動回收站視圖時,工具欄不會出現。另外,選項卡1不顯示全部頁面。回收站視圖中有40個項目,但標籤1顯示39個項目。它並不顯示所有浮動操作按鈕。

我認爲問題的原因是在fragment_tab1.xml中的協調器佈局。因爲當我刪除它時,工具欄消失並在滾動時出現。但浮動操作按鈕不會消失,並在滾動時出現。

這裏是視頻:http://sendvid.com/hyaorcss

activity_main.xml中:

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.fab.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways|snap" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:tabIndicatorColor="@android:color/white" 
      app:tabSelectedTextColor="@android:color/white" 
      app:tabGravity="fill" 
      app:layout_collapseMode="pin" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml:

<RelativeLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.fab.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</RelativeLayout> 

fragment_tab1.xml:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/tab1SwipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/tab1RecyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" 
      android:clickable="true" /> 

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

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:src="@android:drawable/ic_dialog_email" 
     app:layout_anchor="@id/tab1RecyclerView" 
     app:layout_behavior="com.example.fab.ScrollAwareFABBehavior" 
     app:layout_anchorGravity="bottom|right|end" 
     android:id="@+id/emailFab"/> 

</android.support.design.widget.CoordinatorLayout> 

片段_tab2.xml:

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/tab2SwipeRefreshLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tab2RecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" 
     android:clickable="true" /> 

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

回答

0

已解決。

我在activity_main.xml中使用了FloatingActionButton,而不是在fragment_tab1.xml中。

我在主要活動中使ViewPager爲靜態,並將其用於ScrollAwareFABBehavior類。

activity_main。XML:

<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.fab.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways|snap" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:tabIndicatorColor="@android:color/white" 
      app:tabSelectedTextColor="@android:color/white" 
      app:tabGravity="fill" 
      app:layout_collapseMode="pin" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:src="@android:drawable/ic_dialog_email" 
     app:layout_anchor="@id/tab1RecyclerView" 
     app:layout_behavior="com.example.fab.ScrollAwareFABBehavior" 
     app:layout_anchorGravity="bottom|right|end" 
     android:id="@+id/emailFab"/> 
</android.support.design.widget.CoordinatorLayout> 

content_main.xml:

<android.support.v4.view.ViewPager 
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/viewPager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context=".MainActivity" 
tools:showIn="@layout/activity_group_timeline"/> 

fragment_tab1.xml:

<android.support.v4.widget.SwipeRefreshLayout 
android:id="@+id/tab1SwipeRefreshLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="vertical" 
    android:clickable="true" /> 

fragment_tab2.xml:

<android.support.v4.widget.SwipeRefreshLayout 
android:id="@+id/tab2SwipeRefreshLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="vertical" 
    android:clickable="true" /> 

ScrollAwareFABBehavior.java:

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { 

public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { 
    super(); 
} 

@Override 
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, 
            View directTargetChild, View target, int nestedScrollAxes) { 

    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || 
      super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, 
        nestedScrollAxes); 
} 

@Override 
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, 
          View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { 
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, 
      dyUnconsumed); 

    if(MainActivity.viewPager.getCurrentItem() == 0){ 

     if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) { 
      child.hide(); 
     } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { 
      child.show(); 
     } 

    } 

} 
} 
0

對於摺疊工具欄,您似乎缺少可摺疊的工具欄佈局。它看起來像這樣:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/app_bar_height" 
    android:fitsSystemWindows="true" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     ... 

你應該看看這個非常好的app example演示材料設計。它完成了很多你想要做的事情。

+0

我加了摺疊式工具佈局,但沒有奏效。沒有改變。 – cimenmus