1

我在TabLayout/ViewPager內的RecyclerView和一個摺疊工具欄在刷卡過程中有一個奇怪的問題。很難形容請檢查下面的視頻:Android摺疊工具欄,而滾動/刷卡選項卡

https://www.dropbox.com/s/0ilwkqoagtbi67r/device-2015-09-17-150125.mp4?dl=0

當我開始刷卡標籤和手指運動是不是100%的水平我的工具欄開始崩潰,因爲它也註冊我RecyclerView的垂直運動。在動作好像工具欄是跳躍的,你可以看到它在視頻從開始秒6.

這裏是我的佈局:

<?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:context="de.activities.MainActivity"> 
 

 
    <android.support.design.widget.CoordinatorLayout 
 
     android:id="@+id/coordinatorLayout" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
     <android.support.design.widget.AppBarLayout 
 
      android:id="@+id/appBarLayout" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
 

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

 
      <android.support.design.widget.TabLayout 
 
       android:id="@+id/tabLayout" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       app:tabGravity="fill" 
 
       app:tabMode="fixed" 
 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 
 
     </android.support.design.widget.AppBarLayout> 
 

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

 
     <android.support.design.widget.FloatingActionButton 
 
      android:id="@+id/fab" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_gravity="end|bottom" 
 
      android:layout_margin="16dp" 
 
      android:src="@drawable/ic_filter_list_white_24dp" /> 
 
    </android.support.design.widget.CoordinatorLayout> 
 

 
    <android.support.design.widget.NavigationView 
 
     android:id="@+id/navigation_view" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     app:headerLayout="@layout/drawer_header" 
 
     app:menu="@menu/drawer" /> 
 

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

+0

您是否找到解決方案? – dgngulcan

回答

0

編輯 - 剛剛獲得23.1 0.0設計庫,並添加「|彈簧」屬性工具欄佈局:

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_scrollFlags="scroll|enterAlways|snap /> 
     ----- 
     ----- 

因此您不需要使用以下代碼:

您是否嘗試將layout_behaviour屬性添加到AppBarLayout?

app:layout_behavior="AppBarLayoutSnapBehavior" 

然後,你需要創建一個類 「AppBarLayoutSnapBehavior」:

public class AppBarLayoutSnapBehavior extends AppBarLayout.Behavior { 

private ValueAnimator mAnimator; 
private boolean mNestedScrollStarted = false; 

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

@Override 
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, 
            View directTargetChild, View target, int nestedScrollAxes) { 
    mNestedScrollStarted = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); 
    if (mNestedScrollStarted && mAnimator != null) { 
     mAnimator.cancel(); 
    } 
    return mNestedScrollStarted; 
} 

@Override 
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) { 
    super.onStopNestedScroll(coordinatorLayout, child, target); 

    if (!mNestedScrollStarted) { 
     return; 
    } 

    mNestedScrollStarted = false; 

    int scrollRange = child.getTotalScrollRange(); 
    int topOffset = getTopAndBottomOffset(); 

    if (topOffset <= -scrollRange || topOffset >= 0) { 
     // Already fully visible or fully invisible 
     return; 
    } 

    if (topOffset < -(scrollRange/2f)) { 
     // Snap up (to fully invisible) 
     animateOffsetTo(-scrollRange); 
    } else { 
     // Snap down (to fully visible) 
     animateOffsetTo(0); 
    } 
} 

private void animateOffsetTo(int offset) { 
    if (mAnimator == null) { 
     mAnimator = new ValueAnimator(); 
     mAnimator.setInterpolator(new DecelerateInterpolator()); 
     mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
       setTopAndBottomOffset((int) animation.getAnimatedValue()); 
      } 
     }); 
    } else { 
     mAnimator.cancel(); 
    } 

    mAnimator.setIntValues(getTopAndBottomOffset(), offset); 
    mAnimator.start(); 
} 

應該爲你工作。