xamarin/Xamarin.Forms

ListView height calculated wrong after setting new ItemsSource

Open

#4,149 opened on Oct 19, 2018

 (2 comments) (3 reactions) (0 assignees)C# (1,926 forks)batch import
a/listviewe/5 :clock5:help wantedinactivep/iOS 🍎t/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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

Contributor guide