Skip to content

Commit 05804cd

Browse files
committed
fab 按钮滑动隐藏
1 parent 870d800 commit 05804cd

5 files changed

Lines changed: 180 additions & 1 deletion

File tree

diycode-app/src/main/java/com/gcssloop/diycode/activity/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
285285

286286
// 快速返回顶部
287287
private void quickToTop() {
288+
toastShort("快速返回");
288289
switch (mCurrentPosition) {
289290
case 0:
290291
mFragment1.quickToTop();
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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:28
17+
*
18+
* GitHub: https://github.com/GcsSloop
19+
* Website: http://www.gcssloop.com
20+
* Weibo: http://weibo.com/GcsSloop
21+
*/
22+
package com.gcssloop.diycode.widget.behavior;
23+
24+
import android.support.v4.view.ViewCompat;
25+
import android.support.v4.view.ViewPropertyAnimatorListener;
26+
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
27+
import android.view.View;
28+
29+
/**
30+
* Created on 2016/7/14.
31+
*
32+
* @author Yan Zhenjie.
33+
*/
34+
public class AnimatorUtil {
35+
36+
public static final LinearOutSlowInInterpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new LinearOutSlowInInterpolator();
37+
38+
// 显示view
39+
public static void scaleShow(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
40+
view.setVisibility(View.VISIBLE);
41+
ViewCompat.animate(view)
42+
.scaleX(1.0f)
43+
.scaleY(1.0f)
44+
.alpha(1.0f)
45+
.setDuration(800)
46+
.setListener(viewPropertyAnimatorListener)
47+
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
48+
.start();
49+
}
50+
51+
// 隐藏view
52+
public static void scaleHide(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
53+
ViewCompat.animate(view)
54+
.scaleX(0.0f)
55+
.scaleY(0.0f)
56+
.alpha(0.0f)
57+
.setDuration(800)
58+
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
59+
.setListener(viewPropertyAnimatorListener)
60+
.start();
61+
}
62+
63+
// 显示view
64+
public static void translateShow(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
65+
view.setVisibility(View.VISIBLE);
66+
ViewCompat.animate(view)
67+
.translationY(0)
68+
.setDuration(800)
69+
.setListener(viewPropertyAnimatorListener)
70+
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
71+
.start();
72+
}
73+
74+
// 隐藏view
75+
public static void translateHide(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
76+
view.setVisibility(View.VISIBLE);
77+
ViewCompat.animate(view)
78+
.translationY(300)
79+
.setDuration(800)
80+
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
81+
.setListener(viewPropertyAnimatorListener)
82+
.start();
83+
}
84+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

diycode-app/src/main/res/layout/app_bar_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<include layout="@layout/content_main"/>
5656

5757
<android.support.design.widget.FloatingActionButton
58-
app:layout_scrollFlags="scroll|enterAlways"
58+
app:layout_behavior="@string/scale_down_show_behavior"
5959
android:visibility="visible"
6060
android:id="@+id/fab"
6161
android:layout_width="wrap_content"

diycode-app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
-->
2222

2323
<resources>
24+
25+
<string name="scale_down_show_behavior">com.gcssloop.diycode.widget.behavior.ScaleDownShowBehavior</string>
26+
2427
<string name="app_name">Diycode</string>
2528

2629
<string name="navigation_drawer_open">Open navigation drawer</string>

0 commit comments

Comments
 (0)