Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Custom renderer on Android with Content not working

$
0
0

I'm working on a custom renderer that shows content inside a scrollview - based on James Montemagno's parallax idea.

Essentially I have a control that shows an image with a scrollview on top, with content set inside the scrollview that you scroll over the top of the image.

I've managed to get it working in iOS using various tricks, but I'm stuck in Android.

My content is defined on my Forms controls as a View. Inside my Android code I have a layout with a ScrollView, and I add the content to the scrollview by getting the renderer:

var parallaxView = (ParallaxView)Element;
var child = RendererFactory.GetRenderer (parallaxView.Content); 
var type = Type.GetType("Xamarin.Forms.Platform.Android.Platform,Xamarin.Forms.Platform.Android");
if (type != null) {
    var field = type.GetField("RendererProperty", BindingFlags.Static | BindingFlags.Public);
    if (field != null)
        parallaxView.Content.SetValue((BindableProperty)field.GetValue(null), child);
}
_viewGroup = child.ViewGroup;
_contentView.AddView (_viewGroup); // _contentView is a LinearLayout inside a ScrollView with height set to wrap_content

Depending on the content I get one of two things happening:

If it is a Label it sizes to fit and doesn't scroll, despite me manually setting the layout height to wrap_content.
If it is a layout, such as a StackLayout then only the layout is rendered, not the content. E.g. Set the content as an image - I see the image, set the content as a StackLayout with a red background containing the image I just see a red image.

Is there any way to force it to add all the child views? And is there any way to change the layout rules so it sizes everything based on the required size and hence scrolls the contents?

As far as I can tell this is a Forms issue as when I try a pure Android version having a TextView inside a ScrollView with the height set to wrap_content it all works perfectly, so I guess I need to hook into the Forms layout and tweak things.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>