我有一个带有协调器layout.inside活动的活动,有一个带有Recycler视图和浮动按钮的片段。当Scroll Recycler查看并避免使用fab行为时,我可以显示/隐藏浮动按钮吗?!
活动布局中的: CoordinatorLayout -----> AppBarLayout ---->工具栏和FrameLayout和底栏视图
片段布局中的: RelativeLayout ----> Recycler视图和浮动按钮
我想实现Google+主页之类的内容。 我该如何实现这种情况?
在我的片段中使用接口的活动协调器布局,并使用fab行为显示/隐藏fab ...直到找到更好的解决方案!!!
答案 0 :(得分:17)
这段代码很好用:
mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(dy > 0){
mFab.hide();
} else{
mFab.show();
}
super.onScrolled(recyclerView, dx, dy);
}
});
你做不到:
app:layout_anchor="@id/listView"
app:layout_anchorGravity="bottom|end"
看here:
CoordinatorLayout无法内置支持 ListView根据this Google post。
答案 1 :(得分:5)
我修改了Leondro的方法,以便FAB在滚动时隐藏,并在滚动停止时显示。
scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
fab.show();
break;
default:
fab.hide();
break;
}
super.onScrollStateChanged(recyclerView, newState);
}
};
rv.clearOnScrollListeners();
rv.addOnScrollListener(scrollListener);
答案 2 :(得分:2)
将此添加到 FAB :
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
答案 3 :(得分:0)
这是有效的解决方案:
class HideOnScrollFabBehavior(context: Context?, attrs: AttributeSet?) : FloatingActionButton.Behavior() {
// changes visibility from GONE to INVISIBLE when fab is hidden because
// due to CoordinatorLayout.onStartNestedScroll() implementation
// child view's (here, fab) onStartNestedScroll won't be called anymore
// because it's visibility is GONE
private val listener = object : FloatingActionButton.OnVisibilityChangedListener() {
override fun onHidden(fab: FloatingActionButton?) {
fab?.visibility = INVISIBLE
}
}
override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL // Ensure we react to vertical scrolling
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
}
override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, consumed: IntArray) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
if (!target.canScrollVertically(1) && dyConsumed > 0 && child.visibility == VISIBLE) {
// hide the FAB when scroll view reached its bottom
child.hide(listener)
} else if (dyConsumed < 0 && child.visibility != VISIBLE) {
// show the FAB on scroll up
child.show()
}
}
}
答案 4 :(得分:0)
Kotlin 中的解决方案
create1(data): any {
return this.http.post<number>(baseUrl, data).pipe(map(res => {
const resInt = +res;
console.log(resInt);
return resInt;
}),
catchError(error => {
console.log(error);
throw error;
})
);
}