首先是FloatingActionButton的简单使用
首先在xml里声明
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#FFC107"
android:clickable="true"
android:src="@drawable/ic_add_white_24dp"
app:layout_constraintBottom_toBottomOf="@+id/viewPager2_home"
app:layout_constraintEnd_toEndOf="parent"
app:pressedTranslationZ="@dimen/press_button_shadow"
/>
这里是recyclerView滑动的时候不显示悬浮按钮:
这里是recyclerView滑动的属性
SCROLL_STATE_DRAGGING, 先是手指拖拽的状态
SCROLL_STATE_SETTLING,再是手指松开但是RecyclerView还在滑动
SCROLL_STATE_IDLE, 最后是RecyclerView滚动停止状态。
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
floatingActionButton.setVisibility(View.VISIBLE);
break;
case RecyclerView.SCROLL_STATE_SETTLING:
case RecyclerView.SCROLL_STATE_DRAGGING:
floatingActionButton.setVisibility(View.INVISIBLE);
break;
}
}
下面是点击事件
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
来源:CSDN
作者:心境界
链接:https://blog.csdn.net/weixin_43210754/article/details/104755584