Two views issue. Android custom renderer is placed over Xamarin.Forms element

GuyMiccicheGuyMicciche USMember ✭✭
edited June 2017 in Xamarin.Forms

Hello. I have a problem and I do not know how to solve it. I have a custom view LibraryGridView built in Xamarin.Forms which I am using in UWP and iOS. The code works fine and the layout is correct. The view looks like this:

public class LibraryGridView : FlowLayout
{
    public LibraryGridView()
    {
        // code
    }
}

FlowLayout looks like this:

    public class FlowLayout : Layout<View>
    {
        // code

        protected override void LayoutChildren(double x, double y, double width, double height)
        {
            // code
        }

        protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
        {
            // code
        }

        // code
    }

Android renderer looks like this:

public class LibraryGridViewRenderer : ViewRenderer<LibraryGridView, global::Android.Views.View>
{
    // code
}

Now my issue is when I try to create a custom renderer in Android for LibraryGridView. The view works and is lays out correctly. However, I am getting double controls. I get the LibraryGridView control from Xamarin.Forms layed on top of my LibraryGridViewRenderer. How do I make sure for Android I am only showing the LibraryGridViewRenderer and not the Xamarin.Forms LibraryGridView?

I tried to hide the elements in the FlowLayout when running Android, but then the Height and Width of the LibraryGridViewRenderer is lost. Is there a way to fix showing two views, or a way to hide the elements in the Forms view, while keeping the Height and Width of the Renderer? It's a tough question! I spent about 10 hours on this and I have a headache. I included a still frame of the issue.

Thanks for help!

Answers

  • GuyMiccicheGuyMicciche USMember ✭✭

    I know why it's happening, I just don't know how to fix it :neutral:

  • TonyDTonyD USMember ✭✭✭

    The way you've structured it is a bit confusing.

    Is your custom renderer doing layout? If so, maybe in the XF renderer you can just call if (Device.OS == android) return; at the beginning of LayoutChildren

  • Thank you @TonyD that worked for me!

        protected override void LayoutChildren(double x, double y, double width, double height)
            {
                if (Device.OS == TargetPlatform.Android) return;
    
                base.LayoutChildren(x, y, width, height);
            }
    
Sign In or Register to comment.