5

我想要的只是一個簡單的動畫,當scrollview移動時。我嘗試了一些解決方案,但沒有一個能夠完美/平滑地工作。如果我滾動,我想用滑動下來的動畫隱藏晶圓廠,並且如果2秒後沒有任何反應,晶圓廠會顯示滑動動畫。我知道這是一個基本問題,我很欣賞你的病情。Android Floating Action Button動畫

在此先感謝。

final ScrollView scroll = (ScrollView) v.findViewById(R.id.sw); 
scroll.setOnTouchListener(new View.OnTouchListener()){ 
    @Override 
    public boolean onTouch(View v, Motionevent event){ 
     int action = event.getAction(); 
     if (action == Motionevent.ACTION_MOVE){ 

     //slide down animation here 

     }else{ 
     new Handler().postDelayed(new Runnable(){ 
      public void run(){ 

      //slide up animation here 

      } 
     }, 2000); 
     } 
    return false; 
    } 
}); 

回答

11

這裏的a tutorial,如何使用滾動動畫FAB按鈕。

基本上是:

  1. 使用v22.2.1支持V4庫,有show()和浮動操作按鈕
  2. 必須將執行淡入和淡出的動畫hide()方法你ScrollView和CoordinatorLayout中的FAB。
  3. 將FAB layout_anchorScrollView'sid
  4. 創建類和擴展FloatingActionButton.Behavior類並將其設置爲FAB的layout_behavior屬性在佈局XML
  5. 覆蓋您的行爲類onStartNestedScroll檢查是垂直
  6. 覆蓋您的行爲類onStopNestedScroll,以調用子壓縮卷的參數hide()方法和postDelay a Runnable以顯示2秒後的FAB

佈局,如:

<android.support.design.widget.CoordinatorLayout 
... > 
    <ScrollView 
    android:id="@+id/myList" 
    ... 
    /> 
     <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     app:layout_anchor="@id/myList" 
     app:layout_behavior="package.CustomScrollAwareBehavior" 
     ... 
     /> 
    </android.support.design.widget.CoordinatorLayout> 

我建議,也創造Handler到行爲類調用FAB的show()方法。 行爲類像(未測試):

public class CustomScrollAwareBehavior extends FloatingActionButton.Behavior{ 

private Handler handler = new Handler(); 
private FloatingActionButton fab; 

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

@Override 
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, 
            FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { 
    fab = child; 
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || 
      super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, 
        nestedScrollAxes); 
} 



Runnable showRunnable = new Runnable() { 
    @Override 
    public void run() { 
     fab.show(); 
    } 
}; 


@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 (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) { 
     handler.removeCallbacks(showRunnable); 
     handler.postDelayed(showRunnable,2000); 
     child.hide(); 
    } 
    } 
} 
+0

謝謝!!相當準確的答案,我會盡快嘗試這個解決方案。 – tothkris