问题
I have this sample application where I am trying to get the first visible group in the list view whenever I scroll through it. The problem occurs when I scroll up to view the initial items in the list view. It updates the top visible group but it only updates it correct when the list is done scrolling. So suppose if I do flick scrolling (tap,scroll and remove the finger from screen and let it scroll with the inertia given), sometimes, it will be lagging while updating the top visible group value. Here's the link to my sample app: https://1drv.ms/u/s!AhChIerZubKRh3C4DhCZ3K7jpm6u
Edit: I have uploaded the video to show what the issue is. Here you can see that the top text block will only get updated once the scrolling stops completely: https://1drv.ms/v/s!AhChIerZubKRh3pmL6IsQNi0Mrm1
回答1:
The problem is during scrolling, the TextBlock
named "tbHeader"'s position is its real position in the ListView
, it's more like there is a fake one for showing the header(I will say a header TextBlock
in the showing position), and when scrolling stopped, it's like the header TextBlock
is inserted into the header's showing position from its read position.
So here is my solution, we don't find all TextBlock
s named "tbHeader" any more, we can find all ListViewItemPresenter
in this ListView
and find the first showing item, at last show its "DateTimePropertyOfClassA"(header) property.
sv.ViewChanged += (ss, ee) =>
{
//IEnumerable<TextBlock> tblocks = FindVisualChildren<TextBlock>(lv).Where(x => x.Name == "tbHeader");
//if (tblocks != null)
//{
// foreach (TextBlock tblock in tblocks)
// {
// if (IsVisibileToUser(tblock, sv))
// {
// first.Text = tblock.Text;
// break;
// }
// }
//}
IEnumerable<ListViewItemPresenter> presenters = FindVisualChildren<ListViewItemPresenter>(lv);
if (presenters != null)
{
foreach (ListViewItemPresenter presenter in presenters)
{
if (IsVisibileToUser(presenter, sv))
{
var content = presenter.Content as ClassA;
first.Text = content.DateTimePropertyOfClassA.ToString();
break;
}
}
}
};
The other code remain as the last case we discussed.
来源:https://stackoverflow.com/questions/38453039/first-visible-group-in-the-listview-uwp