Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FloatingActionButtonBehavior sample #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions FAB.Sample/FAB.Sample.csproj
Expand Up @@ -65,6 +65,7 @@
<Compile Include="Adapter\LanguageAdapter.cs" />
<Compile Include="SnackActivity.cs" />
<Compile Include="FloatingActionMenuBehavior.cs" />
<Compile Include="FloatingActionButtonBehavior.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
91 changes: 91 additions & 0 deletions FAB.Sample/FloatingActionButtonBehavior.cs
@@ -0,0 +1,91 @@
using System;
using Android.Support.Design.Widget;
using Android.Content;
using Android.Util;
using Clans.Fab;
using Android.Views;
using Android.Support.V4.View;
using Android.Runtime;
using FloatingActionButton = Clans.Fab.FloatingActionButton;

namespace FAB.Demo
{
[Register("FAB.Demo.FloatingActionButtonBehavior")]
public class FloatingActionButtonBehavior : CoordinatorLayout.Behavior
{
private float mTranslationY;

public FloatingActionButtonBehavior(Context context, IAttributeSet attrs)
: base(context, attrs)
{
}

public override bool LayoutDependsOn(CoordinatorLayout parent, Java.Lang.Object child, Android.Views.View dependency)
{
return IsInstanceOf<Snackbar.SnackbarLayout>(dependency);
}

public override bool OnDependentViewChanged(CoordinatorLayout parent, Java.Lang.Object child, Android.Views.View dependency)
{
if (IsInstanceOf<FloatingActionButton>(child) && IsInstanceOf<Snackbar.SnackbarLayout>(dependency))
{
this.UpdateTranslation(parent, (View)child, dependency);
}

return false;
}

public override void OnDependentViewRemoved(CoordinatorLayout parent, Java.Lang.Object child, View dependency)
{
if (IsInstanceOf<FloatingActionButton>(child) && IsInstanceOf<Snackbar.SnackbarLayout>(dependency))
{
this.UpdateTranslation(parent, (View)child, dependency);
}
}

private void UpdateTranslation(CoordinatorLayout parent, View child, View dependency)
{
float translationY = this.GetTranslationY(parent, child);
if (translationY != this.mTranslationY)
{
ViewCompat.Animate(child).Cancel();

if (Math.Abs(translationY - this.mTranslationY) == (float)dependency.Height)
{
ViewCompat.Animate(child)
.TranslationY(translationY)
.SetListener((IViewPropertyAnimatorListener)null);
}
else
{
ViewCompat.SetTranslationY(child, translationY);
}

this.mTranslationY = translationY;
}
}

private float GetTranslationY(CoordinatorLayout parent, View child)
{
float minOffset = 0.0F;
var dependencies = parent.GetDependencies(child);
int i = 0;

for (int z = dependencies.Count; i < z; ++i)
{
View view = (View)dependencies[i];
if (IsInstanceOf<Snackbar.SnackbarLayout>(view) && parent.DoViewsOverlap(child, view))
{
minOffset = Math.Min(minOffset, ViewCompat.GetTranslationY(view) - (float)view.Height);
}
}

return minOffset;
}

private bool IsInstanceOf<T>(object instance)
{
return instance.GetType().IsAssignableFrom(typeof(T));
}
}
}