Listview不会滚动显示单元格,直到滚动回到视图

时间:2018-10-02 21:35:45

标签: xamarin.forms xamarin.forms.listview

Listview导航到时不呈现listview项目。如果我开始滚动,则所有项目都可见。我已经检查了INotityPropertyChanged模型以及列表(Itemsource到listview)。列表视图的XAML代码 `

<ListView x:Name="HospitalResultListView" CachingStrategy="RecycleElement"                 
                          SeparatorVisibility="None"
                          ItemsSource="{Binding HospitalList}" VerticalOptions="FillAndExpand"
                          RowHeight="150" ItemTapped="HospitalResultListView_ItemTapped"  >

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:HospitalListViewVC Address="{Binding Address}" Name="{Binding Name}" Type="{Binding Type}" Rate="{Binding Rate}" Distance="{Binding Distance}" HospitalImageURL="{Binding HospitalImageURL}" />
                    </DataTemplate>
                </ListView.ItemTemplate>

            </ListView>

`

和视单元代码 `

class HospitalListViewVC : ViewCell
    {
        Label addressLabel, distanceLabel, typeLabel, rateLabel, nameLabel;
        Image hospitalImage;
        CachedImage cachedImage = null;

        #region Bindable properties
        public static readonly BindableProperty HospitalImageURLProperty =
            BindableProperty.Create(nameof(HospitalModel.HospitalImageURL), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string HospitalImageURL
        {
            get { return (string)GetValue(HospitalImageURLProperty); }
            set { SetValue(HospitalImageURLProperty, value); }
        }

        public static readonly BindableProperty AddressProperty =
            BindableProperty.Create(nameof(HospitalModel.Address), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string Address
        {
            get { return (string)GetValue(AddressProperty); }
            set { SetValue(AddressProperty, value); }
        }

        public static readonly BindableProperty DistanceProperty =
            BindableProperty.Create(nameof(HospitalModel.Distance), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string Distance
        {
            get { return (string)GetValue(DistanceProperty); }
            set { SetValue(DistanceProperty, value); }
        }

        public static readonly BindableProperty TypeProperty =
            BindableProperty.Create(nameof(HospitalModel.Type), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string Type
        {
            get { return (string)GetValue(TypeProperty); }
            set { SetValue(TypeProperty, value); }
        }

        public static readonly BindableProperty RateProperty =
            BindableProperty.Create(nameof(HospitalModel.Rate), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string Rate
        {
            get { return (string)GetValue(RateProperty); }
            set { SetValue(RateProperty, value); }
        }

        public static readonly BindableProperty NameProperty =
            BindableProperty.Create(nameof(HospitalModel.Name), typeof(string), typeof(HospitalListViewVC), null, BindingMode.OneWay);
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
        #endregion

        #region constructor
        public HospitalListViewVC()
        {
            var grid = new Grid
            {
                Margin = new Thickness(10),

                BackgroundColor = Color.FromHex("#00FFFF"),
                IsClippedToBounds = true,
                ColumnDefinitions = {
                    new ColumnDefinition { Width = new GridLength(4, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
                }
            };

            cachedImage = new CachedImage { Aspect = Aspect.AspectFill};
            nameLabel = new Label();

            addressLabel = new Label();
            distanceLabel = new Label();
            typeLabel = new Label();
            rateLabel = new Label();
            var informationStack = new StackLayout { Padding = new Thickness(5) };
            informationStack.Children.Add(addressLabel);
            informationStack.Children.Add(distanceLabel);
            informationStack.Children.Add(typeLabel);
            informationStack.Children.Add(rateLabel);

            grid.Children.Add(cachedImage, 0, 0);
            grid.Children.Add(nameLabel, 0, 0);
            grid.Children.Add(informationStack, 1, 0);

            View = grid;
        }

        #endregion

        #region Bindings

        protected override void OnBindingContextChanged()
        {
            cachedImage.Source = null;
            base.OnBindingContextChanged();

            if (BindingContext != null)
            {
                cachedImage.Source = HospitalImageURL;
                addressLabel.Text = Address;
                distanceLabel.Text = Distance;
                typeLabel.Text = Type;
                rateLabel.Text = Rate;
                nameLabel.Text = Name;
            }
        }
        #endregion
    }`

任何指针都将真正有用。

附加android模拟器的gif以更好地理解问题 GIF of android emulator

2 个答案:

答案 0 :(得分:0)

我今天有同样的问题。 问题在于,iOS以不同于Android的方式呈现ListView。

请创建一个自定义ListView,以扩展常规ListView并插入以下代码。

public class ListView : Xamarin.Forms.ListView
{
    public ListView() : this(Device.RuntimePlatform == Device.Android ? ListViewCachingStrategy.RecycleElement : ListViewCachingStrategy.RetainElement)
    {
    }

    public ListView(ListViewCachingStrategy cachingStrategy) : base(cachingStrategy)
    {
    }

}

然后在Xaml页面中使用自定义ListView。

区别在这里:

public ListView() : this(Device.RuntimePlatform == Device.Android ? ListViewCachingStrategy.RecycleElement : ListViewCachingStrategy.RetainElement)

我希望这是您的解决方案!

亲切的问候, 尼克

答案 1 :(得分:0)

https://github.com/xamarin/Xamarin.Forms/issues/6810

在寻找解决方案时找到了这篇文章, 看起来他们在Xamarin.Forms 4.0.0 / 4.1.0版本中解决了这个问题