1+ /*
2+ * Copyright 2017 GcsSloop
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *
16+ * Last modified 2017-03-31 03:42:32
17+ *
18+ * GitHub: https://github.com/GcsSloop
19+ * Website: http://www.gcssloop.com
20+ * Weibo: http://weibo.com/GcsSloop
21+ */
22+
23+ package com .gcssloop .diycode .widget .behavior ;
24+
25+ import android .content .Context ;
26+ import android .support .design .widget .CoordinatorLayout ;
27+ import android .support .design .widget .FloatingActionButton ;
28+ import android .support .v4 .view .ViewCompat ;
29+ import android .support .v4 .view .ViewPropertyAnimatorListener ;
30+ import android .util .AttributeSet ;
31+ import android .view .View ;
32+
33+ // FAB 行为控制器
34+ public class ScaleDownShowBehavior extends FloatingActionButton .Behavior {
35+ public ScaleDownShowBehavior (Context context , AttributeSet attrs ) {
36+ super ();
37+ }
38+
39+ @ Override
40+ public boolean onStartNestedScroll (CoordinatorLayout coordinatorLayout ,
41+ FloatingActionButton child , View directTargetChild ,
42+ View target , int nestedScrollAxes ) {
43+ if (nestedScrollAxes == ViewCompat .SCROLL_AXIS_VERTICAL ){
44+ return true ;
45+ }
46+ return super .onStartNestedScroll (coordinatorLayout , child , directTargetChild ,
47+ target , nestedScrollAxes );
48+ }
49+
50+ private boolean isAnimateIng = false ; // 是否正在动画
51+ private boolean isShow = true ; // 是否已经显示
52+
53+ public void onNestedScroll (CoordinatorLayout coordinatorLayout , FloatingActionButton child ,
54+ View target , int dxConsumed , int dyConsumed ,
55+ int dxUnconsumed , int dyUnconsumed ) {
56+ if ((dyConsumed > 0 || dyUnconsumed > 0 ) && !isAnimateIng && isShow ) {// 手指上滑,隐藏FAB
57+ AnimatorUtil .translateHide (child , new StateListener () {
58+ @ Override
59+ public void onAnimationStart (View view ) {
60+ super .onAnimationStart (view );
61+ isShow = false ;
62+ }
63+ });
64+ } else if ((dyConsumed < 0 || dyUnconsumed < 0 && !isAnimateIng && !isShow )) {
65+ AnimatorUtil .translateShow (child , new StateListener () {
66+ @ Override
67+ public void onAnimationStart (View view ) {
68+ super .onAnimationStart (view );
69+ isShow = true ;
70+ }
71+ });// 手指下滑,显示FAB
72+ }
73+ }
74+
75+ class StateListener implements ViewPropertyAnimatorListener {
76+ @ Override
77+ public void onAnimationStart (View view ) {
78+ isAnimateIng = true ;
79+ }
80+
81+ @ Override
82+ public void onAnimationEnd (View view ) {
83+ isAnimateIng = false ;
84+ }
85+
86+ @ Override
87+ public void onAnimationCancel (View view ) {
88+ isAnimateIng = false ;
89+ }
90+ }
91+ }
0 commit comments