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

PullToRefreshContentView problem

$
0
0

This maybe a long shot but is anyone using PullToRefreshContentView,
I place a table view on it to have pull to refresh functionality on android.
It implements this by using a custom render on android.
It was working perfectly until a couple of updates ago, even removing all code from the render it stops the page from displaying back button still works but the page is just blank white, no controls show on it. And it does not though any exceptions.
I have included the render and custom class below.
Can anyone help?

using Xamarin.Forms;

namespace KallidusMobile
{
public class PullToRefreshContentView : ContentView
{
public static readonly BindableProperty IsRefreshingProperty =
BindableProperty.Create<PullToRefreshContentView,bool> (
p => p.IsRefreshing, false);

    public bool IsRefreshing {
        get { return (bool)GetValue (IsRefreshingProperty); }
        set { SetValue (IsRefreshingProperty, value); }
    }

    public static readonly BindableProperty RefreshCommandProperty = 
        BindableProperty.Create<PullToRefreshContentView,Command> (
            p => p.RefreshCommand, null);

    public Command RefreshCommand {
        get { return (Command)GetValue (RefreshCommandProperty); }
        set { SetValue (RefreshCommandProperty, value); }
    }

    /// <param name="widthConstraint">The available width for the element to use.</param>
    /// <param name="heightConstraint">The available height for the element to use.</param>
    /// <summary>
    /// Optimization as we can get the size here of our content all in DIP
    /// </summary>
    protected override SizeRequest OnSizeRequest (double widthConstraint, double heightConstraint)
    {
        if (Content == null)
            return new SizeRequest(new Size(100, 100));

        return Content.GetSizeRequest (widthConstraint, heightConstraint);
    }
}

}

using System;
using Android.Support.V4.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using KallidusMobile;
using KallidusMobile.Android;
using Android.Views;

[assembly:ExportRendererAttribute(typeof(PullToRefreshContentView), typeof(FormsSwipeRefreshLayout))]
namespace KallidusMobile.Android
{
public class FormsSwipeRefreshLayout : SwipeRefreshLayout,
IVisualElementRenderer,
SwipeRefreshLayout.IOnRefreshListener
{
public FormsSwipeRefreshLayout () : base (Forms.Context)
{
}

    public event EventHandler<VisualElementChangedEventArgs> ElementChanged;

    bool init;
    ViewGroup packed;
    /// <summary>
    /// Setup our SwipeRefreshLayout and register for property changed notifications.
    /// </summary>
    /// <param name="element">Element.</param>
    public void SetElement (VisualElement element)
    {
        var oldElement = this.Element;

        //unregister old and re-register new
        if (oldElement != null)
            oldElement.PropertyChanged -= HandlePropertyChanged;

        this.Element = element;
        if (this.Element != null) {
            UpdateContent ();
            this.Element.PropertyChanged += HandlePropertyChanged;
        }

        if (!init) {
            init = true;
            //sizes to match the forms view
            //updates properties, handles visual element properties
            Tracker = new VisualElementTracker (this);
            //add and remove children automatically
            //this is broken right now so I do it manually, but might
            //be fixed in the future.
            //packager = new VisualElementPackager (this);
            //packager.Load ();

            //TODO: this is hardcoded for now via resources, but could be bindable.
            SetColorSchemeResources (Resource.Color.wallet_holo_blue_light,
                Resource.Color.wallet_primary_text_holo_light,
                Resource.Color.material_blue_grey_800,
                Resource.Color.material_blue_grey_900);

            this.SetOnRefreshListener (this);
        }

        if(ElementChanged != null)
            ElementChanged (this, new VisualElementChangedEventArgs (oldElement, this.Element));
    }

    /// <summary>
    /// Managest adding and removing the android viewgroup to our actual swiperefreshlayout
    /// </summary>
    private void UpdateContent()
    {
        if (RefreshView.Content == null)
            return;

        if (packed != null)
            RemoveView (packed);

        var child = RendererFactory.GetRenderer (RefreshView.Content);
        packed = child.ViewGroup;
        AddView (packed);
    }

    /// <summary>
    /// Determines whether this instance can child scroll up.
    /// We do this since the actual swipe refresh can't figure it out
    /// </summary>
    /// <returns><c>true</c> if this instance can child scroll up; otherwise, <c>false</c>.</returns>
    public override bool CanChildScrollUp ()
    {
        var child = packed.GetChildAt (0) as global::Android.Widget.ListView;
        if (child != null) {
            if (child.FirstVisiblePosition == 0) {
                var subChild = child.GetChildAt (0);

                return subChild != null && subChild.Top != 0;
            }

            //if children are in list and we are scrolled a bit... sure you can scroll up
            return true;
        }

        //TODO: handle other View Types here such as scroll view... :(

        //no children
        return false;

    }

    /// <summary>
    /// Helpers to cast our element easily
    /// Will throw an exception if the Element is not correct
    /// </summary>
    /// <value>The refresh view.</value>
    public PullToRefreshContentView RefreshView
    {
        get { return this.Element == null ? null : (PullToRefreshContentView)Element; }
    }

    public override bool Refreshing {
        get {
            return base.Refreshing;
        }
        set {
            //this will break binding :( sad panda we need to wait for next version for this
            //right now you can't update the binding.. so it is 1 way
            //if(RefreshView != null)
            //  RefreshView.IsRefreshing = value;

            base.Refreshing = value;
        }
    }

    /// <summary>
    /// The refresh view has been refreshed
    /// </summary>
    public void OnRefresh ()
    {
        //someone pulled down to refresh or it is done
        if (RefreshView == null)
            return;

        var command = this.RefreshView.RefreshCommand;
        if (command == null)
            return;

        command.Execute (null);
    }

    /// <summary>
    /// Handles the property changed.
    /// Update the control and trigger refreshing
    /// </summary>
    /// <param name="sender">Sender.</param>
    /// <param name="e">E.</param>
    void HandlePropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == PullToRefreshContentView.IsRefreshingProperty.PropertyName) 
        {
            this.Refreshing = this.RefreshView.IsRefreshing;
        } 
        else if (e.PropertyName == "Content") 
        {
            UpdateContent ();
        }
    }

    /// <summary>
    /// Gets the size of the desired.
    /// </summary>
    /// <returns>The desired size.</returns>
    /// <param name="widthConstraint">Width constraint.</param>
    /// <param name="heightConstraint">Height constraint.</param>
    public SizeRequest GetDesiredSize (int widthConstraint, int heightConstraint)
    {
        packed.Measure (widthConstraint, heightConstraint);

        //Measure child here and determine size
        return new SizeRequest (new Size (packed.MeasuredWidth, packed.MeasuredHeight));
    }

    public void UpdateLayout ()
    {
        try{
        if (Tracker == null)
            return;

        Tracker.UpdateLayout ();
        }
        catch(Exception ex) {
            Console.WriteLine (ex.ToString ());
        }
    }

    public VisualElementTracker Tracker {
        get;
        private set;
    }

    public global::Android.Views.ViewGroup ViewGroup {
        get{ return this; }
    }

    public VisualElement Element {
        get;
        private set;
    }
}

}


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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