【发布时间】:2019-07-05 02:03:33
【问题描述】:
我试图让 floatingActionButton 在向下滚动时隐藏并在向上滚动时再次显示,我使用 setOnScrollChangeListener 让 ScrollView 做到这一点
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".DetailsActivity"
android:orientation="vertical">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/linearLayout"
app:layout_anchorGravity="start|bottom"
android:layout_margin="16dp"
android:src="@drawable/icons8_share_480"
/>
代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (scrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
} else {
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int mScrollY = scrollView.getScrollY();
if (mScrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (mScrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
}
the result。
似乎可行,但有时会出现两个问题,当向上滚动 fab 时不会出现第二个问题,它会直接消失而没有任何影响,这个 GIF Destination 越清楚。
【问题讨论】:
标签: android android-layout android-widget android-scrollview floating-action-button