編輯:關於Android編程
先來段Behavior代碼,網上關於FloatingActionButton(以下簡稱FAB)滑動的代碼很多了,參考一下。
public class FabBehavior extends FloatingActionButton.Behavior{
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean mIsAnimatingOut = false;
public FabBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
// Ensure we react to vertical scrolling
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View target, final int dxConsumed, final int dyConsumed,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
// User scrolled down and the FAB is currently visible -> hide the FAB
animateOut(child);
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// User scrolled up and the FAB is currently not visible -> show the FAB
animateIn(child);
}
}
// Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
private void animateOut(final FloatingActionButton button) {
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer()
.setListener(new ViewPropertyAnimatorListener() {
public void onAnimationStart(View view) {
FabBehavior.this.mIsAnimatingOut = true;
}
public void onAnimationCancel(View view) {
FabBehavior.this.mIsAnimatingOut = false;
}
public void onAnimationEnd(View view) {
FabBehavior.this.mIsAnimatingOut = false;
view.setVisibility(View.GONE);
}
}).start();
} else {
}
}
// Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button).translationY(0)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
} else {
}
}
private int getMarginBottom(View v) {
int marginBottom = 0;
final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
}
return marginBottom;
}
}
這是自定義的一個Behavior類,主要在onNestedScroll中自定義了出現和消失的動畫。使用的時候,在xml文件中給FAB加一個包含完整behavior類名的layout_behavior屬性
app:layout_behavior="com.normalframe.widgets.view.FabBehavior"
這樣FAB就會隨著列表上滑消失,下滑出現。這個功能主要是要處理FAB的位置會使列表最後一項被擋住的問題,當上滑時,FAB隱藏,這樣當到達列表底部最後一項時,由於剛剛的動作是上滑動作,所以FAB處於隱藏狀態,不會遮擋到列表。
在寫這個功能時,發現了一個問題:
上滑時FAB能夠正常隱藏,但是下拉列表時,FAB就不出現了。
而一樣的代碼如果放到其它項目中,有些又可以正常實現功能。Debug的時候發現,上拉時會調用onNestedScroll,於是其中自定義的隱藏方法可以被調用,但下滑時,不調用onNestedScroll。
以上所述是小編給大家介紹的Android自定義FloatingActionButton滑動行為只隱藏不出現的問題小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
Android 自定義ProgressDialog示例實現
閒來無事,總結了兩個自定義的ProgressDialog,大家可以參考下,根據自己需要進行選擇修改:實現效果:示例1:示例2: 代碼如下:MainA
Android從零開搞系列:自定義View(6)ScrollTo+ScrollBy+Scroller+NestedScrolling機制(上)
本菜開源的一個自己寫的Demo,希望能給Androider們有所幫助,水平有限,見諒見諒… https://github.com/zhiaixinyang
Android模仿To圈兒個人資料界面層疊淡入淡出顯示效果
前幾天做的一個仿To圈個人資料界面的實現效果下面是To圈的效果Gif圖:做這個東西其實也花了一下午的時間,一開始思路一直沒理清楚,就開始盲目的去做,結果反而事倍功半。以後
Android 6.0: 動態權限管理的解決方案
Android 6.0版本(Api 23)推出了很多新的特性, 大幅提升了用戶體驗, 同時也為程序員帶來新的負擔. 動態權限管理就是這樣, 一方面讓用戶更加容易的控制自己