问题描述
我想实现类似于此示例
但没有工具栏移动和没有自定义视图对于Fab。所以,首先我想看到的布局类似(也可以是简单的LinearLayout放置在屏幕下方有一些子视图),它隐藏当我开始向下滚动列表视图,并显示当我向上滚动一点。已经挖的Web深却一无所获真正起作用。先谢谢了。
but without toolbar move and for custom view not for a FAB. So, firstly i would like to see layout similar to https://www.google.com/design/spec/components/bottom-sheets.html (it can be simple LinearLayout placed on bottom of the screen with some child views) which hides when i start scroll down listview and appears when i scroll up a little. Have digged deep in web but found nothing what really works. Thanks in advance.
推荐答案
首先,你需要扩展默认FAB行为,以保持FAB的行为时,将显示小吃吧
。否则,你会看到它没有转化向上时小吃吧
弹出。
First you need to extend default FAB behavior, in order to keep FAB behavior when Snackbar
is shown. Otherwise you will see it not translating upwards when Snackbar
pops up.
在做出反应垂直滚动只:
React on vertical scrolling only:
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent,
View child, View target, View target,int scrollAxes) {
return (scrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
一旦你有垂直滚动嵌套积累多少已滚动。开始翻译FAB当你滚动尽可能FAB高度:
Once you have vertical nested scrolling accumulate how much has been scrolled. Start translating the FAB when you have scrolled as much as FAB height:
Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dx, int dy, int[] consumed) {
if (dy > 0 && mTotalDy < 0 || dy < 0 && mTotalDy > 0) {
mTotalDy = 0;
}
mTotalDy += dy;
if ( mTotalDy > child.getHeight()
&& child.getVisibility() == View.VISIBLE) {
//translate to it's height, offscreen, set visbility to gone at end of translation animation
} else if (mTotalDy < 0
&& child.getVisibility() == View.GONE) {
//translate to 0 set visbility to visible at end of translation animation
}
}
在 mTotalDy
比FAB的高度,我们向下滚动,更多的时候 mTotalDy
我们正在滚动起来。
When mTotalDy
is greater than FAB height we have scrolling down, when the mTotalDy
we are scrolling up.
您也应该照顾急张嵌套在 onNested preFling()
方法。隐藏FAB当 velocityY&LT; 0
并显示它时, velocityY&GT; 0
,所有这些条件,只有当 Math.abs(velocityY)GT; Math.abs(velocityX)
。换句话说,只有当有垂直急张。
You should also take care of nested flinging in onNestedPreFling()
method. Hide the FAB when the velocityY < 0
and show it when velocityY > 0
, all these conditions only when Math.abs(velocityY) > Math.abs(velocityX)
. In other words, only when there is vertical flinging.
这篇关于与列表视图的自定义布局Android的FAB行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!