Name clash when overriding methods in FloatingActionButton.Behavior

ChrisWayChrisWay Chris WayGBMember
edited July 2015 in Xamarin.Android

Hi Folks

I'm implementing a FAB in my application using the support design library and following this guide to add the disappearing/reappearing when scrolling behaviour. I've converted the ScrollAwareFABBehavior class provided to C# but I get the following compiler error.

error: name clash: onStartNestedScroll(CoordinatorLayout,View,View,View,int) in ScrollAwareFABBehavior and onStartNestedScroll(CoordinatorLayout,V,View,View,int) in Behavior have the same erasure, yet neither overrides the other
public boolean onStartNestedScroll (android.support.design.widget.CoordinatorLayout p0, android.view.View p1, android.view.View p2, android.view.View p3, int p4)
where V is a type-variable:
V extends View declared in class Behavior Project.Droid E:\VS Projects\Project\Project.Droid\obj\Debug\android\src\md5fa22044078b50743372e010f51d93dc4\ScrollAwareFABBehavior.java

Here is the converted class:

public class ScrollAwareFABBehavior : FloatingActionButton.Behavior
{
    public ScrollAwareFABBehavior(Context context, IAttributeSet attrs) {
    }

    public override bool OnStartNestedScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.ScrollAxisVertical ||
            base.OnStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    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 = (FloatingActionButton)child;

        if (dyConsumed > 0 && fab.Visibility == ViewStates.Visible) {
            fab.Hide();
        } else if (dyConsumed < 0 && fab.Visibility != ViewStates.Visible) {
            fab.Show();
        }
    }

}

Does anyone have any ideas how I can fix this?

Best Answer

Answers

  • ArthurAquinoArthurAquino Arthur Aquino BRMember

    Hi. I'm getting the same error. Have you found a solution?

  • ChrisWayChrisWay Chris Way GBMember

    Thanks, that certainly sorts out the compile error! How are you setting this in your axml file? Whatever I try I get a Java Class Not Found Exception.

  • ArthurAquinoArthurAquino Arthur Aquino BRMember

    Hi. Sorry to take so long to respond, I haven't logged in a while. Here's the axml:

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:clickable="true" android:src="@drawable/ic_add_white_24dp" app:layout_anchor="@id/recyclerView" app:layout_anchorGravity="bottom|right|end" app:layout_behavior="md560c1d85c75bfda40cd659a49137b6cb2.ScrollAwareFABBehavior" />

    To locate the class I had to go into ~\obj\Debug\android\src\ and search for the class in each folder. Don't know if there's an easier way. Cleaning and rebuilding the solution generates the same folders, so I think its safe.

  • MikhailMelnikMikhailMelnik Mikhail Melnik AUMember ✭✭

    Did anyone find how to resolve the original issue? Current solution is a good workaround but what if you really need to use behavior other than CoordinatorLayout.Behavior as a base class?

  • CorradoCavalliCorradoCavalli Corrado Cavalli ITInsider, University ✭✭

    Hi folks,
    I'm having the same issue, does anyone has found a solution in the meantime?

  • StphaneEscandellStphaneEscandell Stéphane Escandell FRMember
    edited April 2016

    Hi everyone,

    Thanks @ArthurAquino for this workaround.

    Any idea anyone why we need to:

    1. Necessaraly extend CoordinatorLayout.Behavior (instead of any other one)
    2. Find the package name through generated code to use it in our AXML file

    Any chance to see this fixed?

  • CorradoCavalliCorradoCavalli Corrado Cavalli ITInsider, University ✭✭
    edited April 2016

    I finally got it working, I've blogged about it here http://codeworks.it/blog/?p=460 in case you want to see how I solved.

  • machinezonemachinezone nika USMember

    @StphaneEscandell said:
    Hi everyone,

    Thanks @ArthurAquino for this workaround.

    Any idea anyone why we need to:

    1. Necessaraly extend CoordinatorLayout.Behavior (instead of any other one)
    1. Find the package name through generated code to use it in our AXML file

    Any chance to see this fixed?

    2) You don't need to find the package name through generated code. You can apply RegisterAttribute like this:

    [Android.Runtime.Register("com.example.FabBehavior")]
    public class FabBehavior : FloatingActionButton.Behavior {
       ... 
    }
    

    Refer to breaking changes for Xamarin 5.1 https://developer.xamarin.com/releases/android/xamarin.android_5/xamarin.android_5.1/

  • StphaneEscandellStphaneEscandell Stéphane Escandell FRMember

    Hi @machinezone

    Yes, found that in additionnal test.
    Thanks for your response that could still be usefull for anyone!

  • JonDouglasJonDouglas Jon Douglas USXamarin Team, University, Developer Group Leader Xamurai

    Hi All,

    Just for context on this issue, I have already reported a bug on this and a workaround in the comments:

    https://bugzilla.xamarin.com/show_bug.cgi?id=34777

    Thus the workaround here is to use CoordinatorLayout.Behavior as a base class instead as it's the direct parent of FloatingActionButton.Behavior.

    https://stackoverflow.com/questions/31674372/xamarin-android-name-clash-when-overriding-methods-in-floatingactionbutton-behav

  • gustaveracostagustaveracosta Gustavo Costa GBMember ✭✭

    @CorradoCavalli said:
    I finally got it working, I've blogged about it here http://codeworks.it/blog/?p=460 in case you want to see how I solved.

    Well done!

Sign In or Register to comment.