If the header View is a StackLayout
as follows:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackLayout x:Name="stackLayout"
Grid.Row="0">
...
</StackLayout>
<ListView x:Name="listView"
ItemsSource="{Binding Items}"
Grid.Row="1">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
</Grid>
Xamarin.Forms ListView
provides an OnItemAppearing
event you can subscribe to. With this you can track your scroll direction by finding the index of the item that appeared and comparing it to the last item that appeared. Try something like this:
public partial class MainPage : ContentPage
{
public ObservableCollection<MyItemType> Items { get; set; } = new ObservableCollection<MyItemType>();
int lastItemIndex;
int currentItemIndex;
public MainPage()
{
...
listView.ItemAppearing += ListView_ItemAppearing;
}
async void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
MyItemType item = e.Item as MyItemType;
currentItemIndex = Items.IndexOf(item);
if (currentItemIndex > lastItemIndex)
{
await stackLayout.FadeTo(0, 1000);
stackLayout.IsVisible = false;
}
else
{
stackLayout.IsVisible = true;
await stackLayout.FadeTo(1, 1000);
}
lastItemIndex = currentItemIndex;
}
}
Note: Flickering is really due to ListView
being resized when the StackLayout
shows and hides, so make sure that the ListView
is not getting resized. Perhaps put the ListView
and the StackLayout
in a grid so that when you show and hide the StackLayout
the ListView
does not get resized
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…