daniel-luberda / DLToolkit.Forms.Controls

Xamarin.Forms Custom Controls
Apache License 2.0
393 stars 183 forks source link

Odd behaviour if using s DataTemplateSelector (yea, I know the issues here are just ignored but maybe someone else had the same problem or a solution for it...) #176

Open Sebastian1989101 opened 6 years ago

Sebastian1989101 commented 6 years ago

When using a DataTemplateSelector, the FlowListView Control seems to be randomly choose which template is used for which cell - even if it is 100% clear and correctly served by the template selector (same template instance for additional requests on the same item; not more than 8 on Android).

Here is my current template selector:

public class SearchableDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate ItemTemplate { get; set; }
    public DataTemplate EntryTemplate { get; set; }
    public DataTemplate OtherTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        switch (item)
        {
            case Item _:
                return ItemTemplate;
            case Entry _:
                return EntryTemplate;
            case Other _:
                return OtherTemplate;
        }

        return null;
    }
}

Item, Entry and Other are all Models on it's own and all implement a interface which is the type of my bounded list. But the FlowListView should not care about it anyway and just use the served template for the cell... currently it using it random on the first call (if the result list is a mixed result) and all other results using the same template for the same list index (even if the model on the slot changes it type due to a list modification). I use this for a whole App Search and I want to display each entry with it's own template.

Sebastian1989101 commented 6 years ago

Would be great if @daniel-luberda would care more about the issues and problems of those controls.. But there is simply no option currently except for a self implementation.

c-lamont commented 6 years ago

@Sebastian1989101 I think you need to use FlowTemplateSelector.

I just implemented the selector yesterday and it works fine.

    public class NewsTemplateSelector : FlowTemplateSelector
    {
        protected override DataTemplate OnSelectTemplate(object item, int columnIndex, BindableObject container)
        {
            if (item is NewsListItemModel newsListItemModel)
            {
                switch (newsListItemModel.Type)
                    {
                        case NewsListItemModelType.Normal:
                            return new NewsItemTemplate();
                        case NewsListItemModelType.Header:
                            return new NewsHeaderTemplate();
                        default: throw new NotImplementedException($"{nameof(NewsTemplateSelector)} does not have a template for type: {newsListItemModel.Type}");
                    }
            }

            throw new NotImplementedException($"{nameof(NewsTemplateSelector)} did not receive a {nameof(NewsListItemModel)} item");
        }
    }
Sebastian1989101 commented 6 years ago

@c-lamont Nope dosn't work. Still the same mess. But as you can see above, I use a base type for my list and want a different template for a different type. Also I do not create new templates per entry as you did (because it creates memory leaks, see: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/templates/data-templates/selector/ (because the FLV ist based on a normal LV)). So one of those must be the problem than.

I guess the difference is, that I use the same instance again you don't. But changing this is not an option due to the memory leak potential so it needs to get fixed. But because of the owner does not care about his projects, everyone with this issue needs to fix it on it's own (PR's are also not merged so yea...).

daniel-luberda commented 6 years ago

Sorry, didn't have much time recently to update this repo (it's just a side project, I do other things for a living ) :)

Could you make a repo? I can't reproduce it. I tested it on XF 3.0, so it might have been a Xamarin.Forms issue.