简体   繁体   中英

Reducing the height of horizontal listview - xamarin forms

I am trying to get a horizontal listview scrolling horizontally having some numbers(0-23) as items in it.

I am getting the listview as horizontal but the height of the listview is not getting wrapped to the content. I tried using RelativeLayout with xConstraint, yConstraint, width and heightConstraint, I tried changing the parameter values in it but the listview disappears all the times I change the values.

ListView lv = new ListView
        {
            SeparatorVisibility = SeparatorVisibility.None,
            ItemsSource = items,
            Rotation = 270,

            ItemTemplate = new DataTemplate(() =>
            {

                Label label = new Label()
                {
                    Rotation = 90,
                    TextColor = Color.Black,
                    HorizontalTextAlignment = TextAlignment.Center,
                    FontSize = Device.GetNamedSize(NamedSize.Small, new Label())
                };
                label.SetBinding<User>(Label.TextProperty, indexer => indexer.Time);

                return new ViewCell
                {
                    View = new StackLayout
                    {
                        Children =
                            {
                            label
                        }
                    }
                };
            })
        };

        RelativeLayout rLayout = new RelativeLayout();

        rLayout.Children.Add(lv,
                             xConstraint: Constraint.RelativeToParent((parent) =>
                              {
                                  return (parent.Width / 0.5) - 30;
                              }),
                             yConstraint: Constraint.RelativeToParent((parent) =>
                              {
                                  return (parent.Height / -0.5) + 3;
                              }),
                             widthConstraint: Constraint.Constant(60),
                             heightConstraint: Constraint.RelativeToParent((Parent) =>
                              {
                                  return (Parent.Height / 10);
                              })
        );

        Content = new StackLayout
        {
            Children = {
                rLayout
            }
        };

I am new to xamarin, may be the way I am going is wrong. If so please suggest me the right one.. But as of total I want a horizontal listview having only numbers as items and I want the height to be wrapped to the content.

Please any help would be appreciable.. thanks in advance..

If you want to make auto height of items inside the listview then you have to use HasUnevenRow="true" in your XAML file.

Example:

    <ListView x:Name="SomeList" HasUnevenRows="True" ItemsSource="{Binding ...}" SeparatorVisibility="None">
       <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <Label HorizontalOptions="CenterAndExpand" TextColor="Blue" Margin="0,5,0,5" Text="{Binding ....}" FontSize="Small" HeightRequest="35"/>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM