我有一个屏幕ListView
显示评论集合。另外,我有一个StackLayout
与末端重叠ListView
,其中有一个Entry
and a Button
添加新评论。
我想隐藏/显示这个StackLayout
取决于ListView
滚动方向:
- 如果用户向下滚动 -> 隐藏
StackLayout
.
- 如果用户向上滚动 -> 显示
StackLayout
.
有人知道实现这种行为的方法吗?
提前致谢!
Xamarin.FormsListView
提供了一个OnItemAppearing
您可以订阅的活动。这样,您可以通过查找出现的项目的索引并将其与最后出现的项目进行比较来跟踪滚动方向。尝试这样的事情:
public partial class MainPage : ContentPage
{
public ObservableCollection<MyItemType> Items { get; set; } = new ObservableCollection<MyItemType>();
int lastItemIndex;
int currentItemIndex;
public MainPage()
{
...
listView.ItemAppearing += ListView_ItemAppearing;
}
void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
MyItemType item = e.Item as MyItemType;
currentItemIndex = Items.IndexOf(item);
if (currentItemIndex > lastItemIndex)
{
stackLayout.IsVisible = false;
}
else
{
stackLayout.IsVisible = true;
}
lastItemIndex = currentItemIndex;
}
}
EDIT:闪烁确实是由于ListView
正在调整大小时StackLayout
显示和隐藏,因此请确保ListView
没有调整大小。也许将 ListView 和 StackLayout 放在网格中,以便当您显示和隐藏时StackLayout
the ListView
不会调整大小,例如:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<ListView x:Name="listView"
ItemsSource="{Binding Items}"
Grid.Row="0">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
<StackLayout x:Name="stackLayout"
Grid.Row="1">
...
</StackLayout>
</Grid>
通过上述操作,不再出现闪烁现象。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)