xamarin/Xamarin.Forms

ListView height calculated wrong after setting new ItemsSource

开放

#4,149 创建于 2018年10月19日

 (2 条评论) (3 个反应) (0 位负责人)C# (1,926 个派生)batch import
a/listviewe/5 :clock5:help wantedinactivep/iOS 🍎t/bug :bug:up-for-grabs

仓库指标

星标
 (5,644 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

Description

Empty ListView or ListView with few items has height calculated incorrectly after swapping ItemsSource if the ListView's available space changes

Steps to Reproduce

This bug happens whenever the ListView is set to a new itemSource after the container's height is recalculated.

  1. Create a ListView with VerticalOptions = FillAndExpand and a RowHeight
  2. Make the ListView recalculate its height by any means (adding content, changing text, etc)
  3. Swap the ListView's itemSource to an empty collection or a collection with not enough items to fill all available height.

Steps 2 and 3 can be swapped in order.

Example:

    public class TestPage : ContentPage
    {
        public Xamarin.Forms.ListView _listView;
        public StackLayout _myStack;
        public Label _myLabel;

        public TestPage()
        {
            _listView = new ListView //You can set any ItemTemplate you want, it doesn't affect this issue
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                RowHeight = 230,
                IsPullToRefreshEnabled = true,
                BackgroundColor = Color.Pink
            };

            _myLabel = new Label()
            {
                Text = "test"
            };

            _myStack = new StackLayout()
            {
                BackgroundColor = Color.Yellow
            };

            Content = new StackLayout()
            {
                Orientation = StackOrientation.Vertical,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children =
                {
                    _myStack,
                    _listView
                }
            };
        }

        protected override void OnAppearing()
        { 
            base.OnAppearing(); 
            _listView.ItemsSource = new List<object>();
            _myStack.Children.Add(_myLabel); 
        }
    }

Expected Behavior

ListView Vertical FillAndExpand

Actual Behavior

ListView height is set to however many items are in the new itemsSource.

If the itemSource is empty, the ListView will be set to 0 height.

If the itemSource has 1 item, the ListView will be set to the height of 1 item, etc.

Basic Information

  • Version with issue: 3.1.0.697729
  • Last known good version: Not known
  • IDE: Visual Studio 2017
  • Platform Target Frameworks:
    • iOS:
    • Android:
  • Android Support Library Version: Android 8.0
  • Nuget Packages: Xamarin.Forms
  • Affected Devices: All Android and iOS devices

Screenshots

Reproduction Link

贡献者指南