【问题标题】:How to hide the empty rows of a list view in xamarin.forms如何在 xamarin.forms 中隐藏列表视图的空行
【发布时间】:2017-11-14 00:48:27
【问题描述】:

我有一个StackLayout 和一个ListView,我有一个添加按钮,我想在ListView 正下方显示它。但是ListView 显示了许多未使用的行。只是空行,这迫使我的按钮显示在页面底部。我整天都在搞乱VerticalOptions,但无法让行消失。

这是我的 XAML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:LiquitMobileApp"
             x:Class="LiquitMobileApp.MainPage"
             Title="Settings"
             >

    <StackLayout BackgroundColor="LightGray">
        <Label Text="Liquit Zones" TextColor="Gray" FontSize="Small" Margin="10"/>
        <StackLayout AbsoluteLayout.LayoutBounds="10,10,10,10">
            <ListView x:Name="UsingZone" SeparatorColor="LightGray" ItemTapped="ZonesList_ItemTapped" RowHeight="60" BackgroundColor="Green"  >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem Clicked="OnEdit" Text="Edit" />
                                <MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Trash" IsDestructive="True" />
                            </ViewCell.ContextActions>
                            <StackLayout Padding="15, 5, 0, 0" Orientation="Horizontal" BackgroundColor="White">
                                <Image Source="{Binding Image}" HorizontalOptions="Start" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/>
                                <StackLayout Orientation="Vertical">
                                <Label Text = "{Binding Name}" FontSize="20" TextColor="Black" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                                <Label Text = "{Binding Address}" TextColor="LightGray" AbsoluteLayout.LayoutBounds="50, 35, 200, 25"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.Footer>
                    <Label />
                </ListView.Footer>
            </ListView>
            <Button Text="Add Zone" TextColor="Black" HorizontalOptions="FillAndExpand" FontSize="Medium" Clicked="Button_Clicked" BackgroundColor="White"/>
        </StackLayout>
    </StackLayout>
</ContentPage>

列表和按钮的图片:

【问题讨论】:

  • 我猜问题出在 ListView 的高度上。如果您没有明确指定,它假定一个默认值太大,当我们很少有项目时......我正在努力解决我的列表视图的 HeightRequest 属性,而没有找到更好的解决方案
  • 空行是从绑定的数据源加载的还是默认创建的?
  • @KrzysztofBracha 它们是默认创建的。
  • 如果您将列表高度控制为仅填充行,您对添加按钮的期望是什么,根据列表高度在列表下方上下跳转?
  • @YuriS 我已经编辑了我的帖子,你现在可以看到列表和添加按钮,我希望绿色行消失并且添加按钮显示在最后一个 ListItem 下方

标签: c# xaml listview xamarin.forms portable-class-library


【解决方案1】:

删除 ListView 的空行的一种简单方法是为 ListView 设置一个空的页脚。

ListView HeaderList = new ListView()
    {
       Footer = ""
    };

或在 XAML 中

<ListView  x:Name="HeaderList" Footer=""></ListView>

【讨论】:

    【解决方案2】:

    所以,我找到的解决方案是每次添加/删除项目时强制列表视图布局。 您还需要将列表包装在 stacklayout 中。

    <StackLayout BackgroundColor="LightGray">
        <Label Text="Liquit Zones" TextColor="Gray" FontSize="Small" Margin="10"/>
    
        <StackLayout>
        <ListView x:Name="ItemsListView" SeparatorColor="LightGray" BackgroundColor="Green" RowHeight="60" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Clicked="AddItemClicked" Text="Add" />
                            <MenuItem Clicked="RemoveItemClicked" CommandParameter="{Binding .}" Text="Trash" IsDestructive="True" />
                        </ViewCell.ContextActions>
                        <StackLayout Padding="15, 5, 0, 0" Orientation="Horizontal" BackgroundColor="White">
                            <Image Source="{Binding Image}" HorizontalOptions="Start"/>
                            <StackLayout Orientation="Vertical">
                                <Label Text = "{Binding ItemText}" FontSize="20" TextColor="Black" />
                                <Label Text = "{Binding ItemDetail}" TextColor="LightGray" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Footer>
                <Label />
            </ListView.Footer>
        </ListView>
        </StackLayout>
    
        <Button Text="Add Item" TextColor="Black" HorizontalOptions="FillAndExpand" FontSize="Medium" Clicked="AddItemClicked" BackgroundColor="White"/>
    </StackLayout>
    

    因为你知道你的细胞高度,你可以这样做:

           void AddItemClicked(object sender, EventArgs e)
            {
                SampleData data = new SampleData();
                data.ItemText = "An Item";
                data.ItemDetail = "Item - " + (itemsListCollection.Count + 1).ToString();
                itemsListCollection.Add(data);
    
                ItemsListView.HeightRequest = itemsListCollection.Count * 60;
                //ForceLayout();
            }
    
        void RemoveItemClicked(object sender, EventArgs e)
        {
    
            SampleData item =(SampleData)((MenuItem)sender).CommandParameter;
            if (item != null)
            {
                itemsListCollection.Remove(item);
            }
            ItemsListView.HeightRequest = itemsListCollection.Count * 60;
    
        }
    
    
    class SampleData:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string itemText;
        public string ItemText
        {
            get
            {
                return itemText;
            }
            set
            {
                itemText = value;
                NotifyPropertyChanged("ItemText");
            }
        }
        private string itemDetail;
        public string ItemDetail
        {
            get
            {
                return itemDetail;
            }
            set
            {
                itemDetail = value;
                NotifyPropertyChanged("ItemDetail");
            }
        }
        private void NotifyPropertyChanged(string propName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propName));
            }
        }
    }
    

    【讨论】:

    • 谢谢!这为我做了:ItemsListView.HeightRequest = itemsListCollection.Count * 60;
    • 我怎样才能使60动态确定?
    • 不确定你的意思
    【解决方案3】:

    大家好,

     public class Test
     {
        string Name{get;set;}
     }
    

    全局声明

      ObservableCollection<Test> objWithEmpty;
    

    在构造函数中:

     objWithEmpty=new ObservableCollection<Test>();
     objWithEmpty.Add(new Test(){"Venky"});
     objWithEmpty.Add(new Test(){""});
     objWithEmpty.Add(new Test(){"Raju"});
    

    现在再取一个 ObservableCollection 并根据您的要求检查条件而不为空,然后将该项目添加到 objWithOutEmpty Collection 然后最后将 objWithOutEmpty ItemsSource 添加到 ListView。

      ObservableCollection<Test> objWithOutEmpty;
    
     foreach(var item in objWithEmpty)
       {
            if(item.Name !=null)
               {
                      objWithOutEmpty.Add(new Test(){item.Name});
               }
       };
    

    现在将 ItemsSource 设置为 ListView

     listView.ItemsSource=objWithOutEmpty;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多