F.A.B隐藏但不显示 [英] F.A.B Hides but Doesn't Show
问题描述
试图实现Floating Action Button
(F.A.B),该Floating Action Button
在向下滚动时隐藏,而在向上滚动时显示.
Trying to implement a Floating Action Button
(F.A.B) that hides on scroll down, and shows on scroll up.
我有ScrollAwareFABBehavior.java
来管理它,它以XML activity_main
的形式连接到F.A.B. 问题:F.A.B在向下滚动时隐藏,但在向上滚动时不再显示.我记录了onNestedScroll
方法,它在向下滚动时称为调用滚动"和调用隐藏".但是在隐藏F.A.B之后,没有3x Log
的
I have ScrollAwareFABBehavior.java
to manage this, and it's connected to the F.A.B in in the XML activity_main
. Problem: The F.A.B hides on scroll down, but doesn't show again when I scroll up. I logged the onNestedScroll
method and it calls "calling scroll" and "calling to hide" while scrolling down; but after the F.A.B is hidden there are none of the 3x Log
's
问题:隐藏F.A.B后,为什么向上滚动时F.A.B不显示.
ScrollAwareFABBehavior.java:
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
// Ensure we react to vertical scrolling
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View target, final int dxConsumed, final int dyConsumed,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
Log.d("test", "calling scroll");
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
// User scrolled down and the FAB is currently visible -> hide the FAB
Log.d("test", "calling to hide");
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// User scrolled up and the FAB is currently not visible -> show the FAB
Log.d("test", "calling to show");
child.show();
}
}
}
activity_main.xml:
<android.support.design.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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="companyname.appname.MainActivity">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_contactlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add_circle_outline_black_24dp"
app:layout_anchor="@id/rv_contactlist"
app:layout_anchorGravity="bottom|right|end"
android:layout_alignParentEnd="true"
app:fabSize="normal"
android:layout_alignParentBottom="true"
android:onClick="addItem"
app:layout_behavior="companyname.appname.ScrollAwareFABBehavior"/>
</android.support.design.widget.CoordinatorLayout>
推荐答案
您的项目中使用了哪个支持库版本?
Which support library version are you using in your project?
如果使用的是最新版本(我的意思是25.0.x),这是因为fab.hide()方法将可见性设置为View.GONE.这会使嵌套的滚动侦听器停止检查fab,这是您第二次尝试滚动列表.
If you are using the latest one ( I mean 25.0.x), this is because fab.hide() method set the visibility to be View.GONE. This makes nested scroll listener stop to check fab, the second time you try to scroll the list.
可在此处找到更多详细信息: https://code.google .com/p/android/issues/detail?id = 230298
More detail can be found here: https://code.google.com/p/android/issues/detail?id=230298
我搜索了一下,发现这个类似的问题已经有了一个不错的答案: 浮动操作更新Google支持和服务后,滚动时该按钮不可见设计库
And I search a bit found this similar question already have a nice answer: Floating action button not visible on scrolling after updating Google Support & Design Library
因此可能的解决方法是重写fab.hide方法,而不是将可见性设置为GONE,而是将INVISIBLE设置为可见.
So a possible work around would be to override fab.hide method, not to set the visibility to GONE but INVISIBLE instead.
而且我认为以后可能会从上游修复此问题,因此请注意一下.
And I think this may be fixed from upstream later, so just keep an eye on it.
这篇关于F.A.B隐藏但不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!