Search code examples
xamarin.formsxamarin.androidxamarin.ioscustom-renderer

Change loading indicator Offset in Xamarin.Forms RefreshView


On Xamarin.Forms (iOS Android), I need to change the loading-indicator position on the RefreshView. I need to add offset, so the indicator is visible if you have a bar overlapping the ScrollView-RefreshView combo and trigger pullOnRefresh.

Loading-indicator in the top edge

EDIT: thanks to Junior Jiang - MSFT- Android solution

I also implement a solution for xamarin.iOS

[assembly: ExportRenderer(typeof(RefreshView), typeof(CustomRefreshViewRenderer))] 
namespace CustomRefresh.iOS {
public class CustomRefreshViewRenderer : RefreshViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<RefreshView> e)
    {
        base.OnElementChanged(e);

        foreach (var nativeView in Subviews)
            updateRefreshSettings(nativeView);

    }

    void updateRefreshSettings(UIView view) {
        if (view is UIScrollView)
        {
            var scrollView = view as UIScrollView;
            if (scrollView.RefreshControl != null)
            {
                var bounds = scrollView.RefreshControl.Bounds;
                scrollView.RefreshControl.Bounds = new CGRect(bounds.X, -(100), bounds.Width, bounds.Height);
            }
        }
        //add more scrollable view types
    }
}

}


Solution

  • You can custom a RefreshViewRenderer to achieve that .

    In Android , there is a MOriginalOffsetTop to modify the offset of loading indicator . In addition , also can use SetProgressViewOffset to set the start and end position of indicator .

    Code as follow :

    using Android.Content;
    using RefreshViewDemo.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    [assembly: ExportRenderer(typeof(RefreshView), typeof(CustomRefreshViewRenderer))]
    namespace RefreshViewDemo.Droid
    {
        public class CustomRefreshViewRenderer : RefreshViewRenderer
        {
    
            public CustomRefreshViewRenderer(Context context) : base(context)
            {
                MOriginalOffsetTop = 100;
    
               // SetProgressViewOffset(true, 100, 101);
            }
    
        }
    }
    

    In iOS , the loading indicator belong to UIRefreshControl of UIScrollView , there is no direct way to change it's offset . Unless override all content view in Renderer then can achieve that . You can refer this Xamarin.Forms/Xamarin.Forms.Platform.iOS/Renderers/RefreshViewRenderer.cs to know what the RefreshView is made of .

    ====================Update=====================

    Shared code is based on this official sample :https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-refreshviewdemo/

    For Android , just need to create a CustomRefreshViewRenderer class in android solution .

    The effect of SetProgressViewOffset(true, 100, 101) , it seems like the indicator not moving :

    enter image description here