我正在使用 C#Xamarin.Android
GITHUB - jamesmontemagno/FloatingActionButton-for-Xamarin.Android
现在这个库已被弃用了,我还没有找到如何将我的fab按钮附加到recyclerview:当向上或向下滚动时我想要fab按钮出现并消失..
这就是github代码的方式。但今天不起作用
var listView = FindViewById<ListView>(Android.Resource.Id.list);
var fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.AttachToListView(listView);
我的XML:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_marginTop="100dp"
android:layout_height="match_parent"
android:layout_below="@+id/buildpizzaingredienti_toolbar">
<android.support.v7.widget.RecyclerView
android:id="@+id/buildpizzaingredienti_cmListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_next"
app:backgroundTint="#4A8E53" />
答案 0 :(得分:1)
有一个示例如何在FloatingActionButton
Animating an Android Floating Action Button上添加动画
但我想也许你会在C#中翻译Java代码时遇到问题所以我很快就做到了。
class FAB_Hide_on_Scroll : FloatingActionButton.Behavior
{
public FAB_Hide_on_Scroll(Context context, IAttributeSet attr) : base()
{
}
public override void OnNestedScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
{
base.OnNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
var fab = child.JavaCast<FloatingActionButton>();
if (fab.Visibility == ViewStates.Visible && dyConsumed > 0)
{
fab.Hide();
}
else if (fab.Visibility == ViewStates.Gone && dyConsumed < 0)
{
fab.Show();
}
}
public override bool OnStartNestedScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object child, View directTargetChild, View target, int nestedScrollAxes)
{
return nestedScrollAxes == ViewCompat.ScrollAxisVertical;
}
}
然后,您必须在FloatingActionButton
行为中包含此类以获得所需的行为。像这样:
app:layout_behavior="com.yourpackagename.FAB_Hide_on_Scroll"
我不确定您的Recyclerview
和FloatingActionButton
容器或父级必须是CoordinatorLayout
。请注意,我没有测试这段代码,所以我不知道它会抛出一些你必须要做的错误。
答案 1 :(得分:0)
基于较新版本进行更新:
public class FabBehavior : CoordinatorLayout.Behavior
{
public FabBehavior()
{
}
public FabBehavior(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public override void OnNestedScroll(CoordinatorLayout coordinatorLayout, Object child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
int dyUnconsumed, int type)
{
base.OnNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
var fab = child.JavaCast<FloatingActionButton>();
if (fab.Visibility == ViewStates.Visible && dyConsumed > 0)
{
fab.Hide(new VisibilityListener());
}
else if (fab.Visibility == ViewStates.Invisible && dyConsumed < 0)
{
fab.Show();
}
}
public override bool OnStartNestedScroll(CoordinatorLayout coordinatorLayout, Object child, View directTargetChild, View target, int axes, int type)
{
return axes == ViewCompat.ScrollAxisVertical;
}
}
public class VisibilityListener : FloatingActionButton.OnVisibilityChangedListener
{
public override void OnHidden(FloatingActionButton fab)
{
base.OnHidden(fab);
fab.Visibility = ViewStates.Invisible;
}
}
更新:
我不知道为什么,但是该课程有效:
FabBehavior : CoordinatorLayout.Behavior
因为Hide()
将ViewStates.Gone
设置为fab,并且ConstraintLayout
不再看到此按钮。伤心
需要将Gone
更改为Invisible
if (fab.Visibility == ViewStates.Invisible && dyConsumed < 0)
需要添加VisivilityListener
进行隐藏操作。在其中设置Invisible
代替Gone
fab.Hide(new VisibilityListener());