Forum Xamarin.Forms

Using a custom listview adapter with Xamarin Forms ViewCell

Hi,

I'm trying to create a custom Android listview implementation and want to render the Xamarin Forms ViewCells.

So in the GetView method of ListAdapter I use "Platform.CreateRenderer(view);" to try and render the viewcell. For some reason this is working only when ViewCell only has one element in it (e.g. a button). When I add a layout it will only render the layout but not its children

     public override View GetView(int position, View convertView, ViewGroup parent)
        {
            OBTestCell testCell = new OBTestCell();
            var view = RendererFactory.GetRenderer(testCell.View);
            view.ViewGroup.LayoutParameters = new global::Android.Widget.GridView.LayoutParams(600, 300);
            return view;
        }

This is working:

    public class OBTestCell: ViewCell
    {
        public OBTestCell()
        {
            this.View = new Button() { Text = "Wooooooooot" };; 
        }
    }

This will just show a green background

  public class OBTestCell: ViewCell
    {
        public OBTestCell()
        {
            StackLayout layout = new StackLayout();
            layout.BackgroundColor = Color.Lime;
            layout.VerticalOptions = LayoutOptions.FillAndExpand;
            layout.HeightRequest = 200;

            layout.Children.Add(new Button() { Text = "Wooooooooot" });
            layout.Children.Add(new Button() { Text = "Wooooooooot" });
            this.View = layout; 
        }
   } 

Any idea whats's wrong or any working method of using a custom listview adapter with Xamarin Forms ViewCells?

Posts

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    You should clone the Xamarin.Forms samples. These include many sample projects and explain how to do some things you won't find on the web site.
    What you're particularly interested in is WorkingWithListviewNative.
    In short: calling RendererFactory.GetRenderer directly is the wrong approach. It is meant to be used in XF internally only.

  • BenjaminGmeinerBenjaminGmeiner USMember ✭✭

    So how do I reuse the Xamarin Forms ViewCells? I'm aware that I can do the implementation all on the Android side, but then I would have to write a renderer for iOS too. What's the point of Xamarin Forms then?

    Maybe I'm missing something here, but I see no other way of doing what I need to do (drag-and-drop listview) on Android without replacing the whole listview+adapter and thus I need a way to render the xamarin forms viewcells.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    The ViewCellRenderer has a GetCell method which returns the native Android.Views.View. So you could call into that from your adapters GetView method.
    But getting everything right won't be easy. The best documentation about this is the WorkingWithListviewNative sample. For everything else you'll have to use a decompiler and look at the XF dlls directly.

  • DavidDunscombeDavidDunscombe GBMember ✭✭
    edited February 2016

    If your trying to do your own container, make sure your viewcell Parent property is set to the listview after your create it from the template otherwise you will have layout problems with its children. It is also possible to do some reflection 'hacks' to set the private Renderer property of the viewcell instead, but the setting the Parent 'seems to work'...

    As mentioned though, doing all this is certainly in the area of unsupported as far as Xamarin is concerned, which is shame because writing my own custom container is something I would like to do in a more legit way.

  • BenjaminGmeinerBenjaminGmeiner USMember ✭✭

    Hi,

    I've tried to set the parent but this doesn't help either...

    I've extended the WorkingWithListviewNative sample to show what I'm trying to do and how it isn't working (source code attached)

    I think it has to be possible somehow because it seems to be implemented in the Telerik ListView component but their source code is not available and they're doing a lot of custom stuff: http://docs.telerik.com/devtools/xamarin/controls/listview/listview-getting-started

  • DavidDunscombeDavidDunscombe GBMember ✭✭

    I forgot you will also have to wrap it in a container viewgroup so you can get capture the android layout call. I've attached the modded adapter. The container in the zip also does a measure so this will use the viewcell heightrequest directly, or should just wrap its contents if you don't specify a height.

  • BenjaminGmeinerBenjaminGmeiner USMember ✭✭

    Wow that really seems to work,

    thank you very much David!!!!

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    @DavidDunscombe Thank you very much

  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    i think this will help You........

Sign In or Register to comment.