当我在“活动”中向下滚动但不起作用时,我正试图隐藏我的FAB。为什么???片段标签内部包含RecyclerView片段。
这是我的Activity xml代码:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.principal.PrincipalActivity">
<fragment
android:id="@+id/PrincipalActivity_navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/principal_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/PrincipalActivity_bottom_navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@android:color/white"
app:layout_anchor="@id/PrincipalActivity_navHostFragment"
app:layout_anchorGravity="bottom"
app:menu="@menu/bottom_navigation_menu_principal" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/PrincipalActivity_fabItensPedido"
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="70dp"
android:src="@drawable/ic_carrinho"
app:backgroundTint="@color/colorAccent"
app:fabSize="auto"
app:layout_anchor="@id/PrincipalActivity_navHostFragment"
app:layout_anchorGravity="bottom|end"
app:layout_behavior=".utils.ScrollAnimationFAB" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是我在FAB按钮中使用的布局行为,但不适用于活动,仅在片段标记内的片段中有效。
class ScrollAnimationFAB extends CoordinatorLayout.Behavior<FloatingActionButton> {
public ScrollAnimationFAB(){
super();
}
public ScrollAnimationFAB(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
axes, ViewCompat.TYPE_TOUCH);
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull int[] consumed) {
Log.v("Msg", "dyConsumed: " + dyConsumed + ", dyUnConsumed: " + dyUnconsumed);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
fab.setVisibility(View.INVISIBLE);
}
});
} else if (dyConsumed < 0 && dyUnconsumed == 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
}
请帮助我。
浮动操作按钮为您提供了一种很好的方法fab.hide()
如果FloatingActionButton位于CoordinatorLayout中-您应使用以下代码:
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) playlist_FAB_start.getLayoutParams();
p.setAnchorId(View.NO_ID);
playlist_FAB_start.setLayoutParams(p);
playlist_FAB_start.setVisibility(View.GONE);