Search code examples
androiddata-bindingmvvmcross

Reusing items in MvxListView


I'm wondering how MvxAdapter handles viewToUse in GetBindableView. I'm aware that it changes data context of that view but what does it mean? If we have some TextView:

<TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/warning_item_height"
        local:MvxBind="Text SomeTextProperty; Visibility IsRemoved, Converter=Visibility" />

will context change first determine if IsRemoved property in old item is equal to that in new item ...and possibly avoid using converter? It is maybe irrelevant in this example but it becomes an issue if we are trying to hide some picture. Will it redraw each time?

And if there's no Converter ... if we have just plain MvxImageView ?

<Mvx.MvxImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="fitCenter"
    local:MvxBind="AssetImagePath SomeImagePath" />

Will it redraw each time?


Solution

  • When reusing a list item, the binding will set the DataContext again, and this will cause the bindings to be reevaluated.

    will context change first determine if IsRemoved property in old item is equal to that in new item ...and possibly avoid using converter?

    No - the previous state of the IsRemoved property isn't cached anywhere - so the converter will be called.

    Will it redraw each time?

    If the property of the control doesn't change, and if the control itself checks this, then redrawing can be avoided - e.g. for MvxImageView on Android, it checks for value == existing in MvxDynamicImageHelper - see https://github.com/MvvmCross/MvvmCross/blob/bbf9a2ac76e74d9404f4b57036c6e29dfe2cc6c3/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache/MvxDynamicImageHelper.cs#L73

    Note:

    • that there is also one edge case here - if the list item view is removed from visibility then the adapter will set its DataContext to null - so if that list item view is then later reused, then the value will transition via empty on its way back to reused.

    • that if any app needs to optimise behaviour, then it's straight-forward to use custom controls to assist with this - e.g. inheriting from MvxImageView and providing a custom MyAssetImagePath property to bind to.