Created
January 13, 2014 21:08
-
-
Save ceee/8408175 to your computer and use it in GitHub Desktop.
Infinite scrolling or LongListSelector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<phone:LongListSelector Name="ItemList" | |
ItemsSource="{Binding Items}" | |
ItemRealized="ItemList_ItemRealized"> <!-- this is the important listener for the scrolling --> | |
<!-- your items --> | |
</phone:LongListSelector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the offset knob defines after how many "fresh" loaded items (which get visible while scrolling) | |
// the list should load new items | |
private int _offsetKnob = 2; | |
// this is the current page number | |
public int PageNumber { get; set; } | |
public YourListControl() | |
{ | |
// set page number to first page | |
PageNumber = 0; | |
InitializeComponent(); | |
} | |
// this listener listens to the event, which is triggered when new items are realized. | |
// While scrolling new items are shown in the viewport -> so they are realized | |
// just play with the offsetKnob property to get the correct behavior | |
private async void ItemList_ItemRealized(object sender, Microsoft.Phone.Controls.ItemRealizationEventArgs e) | |
{ | |
// in case the items are not null | |
// and the items are more than the offset knob | |
// and they are actual items (could be a list footer too) | |
if (ItemList.ItemsSource != null && ItemList.ItemsSource.Count >= _offsetKnob && e.ItemKind == LongListSelectorItemKind.Item) | |
{ | |
// check if you hit the last item, after which you want to trigger the loading of new items | |
// that's where you need the offsetKnob | |
if ((e.Container.Content as Item).Equals(ItemList.ItemsSource[ItemList.ItemsSource.Count - _offsetKnob])) | |
{ | |
// load new items and increment PageNumber afterwards | |
await (DataContext as MainViewModel).UpdateLists(++PageNumber); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// your items | |
public ObservableCollection<Item> Items { get; set; } | |
// your items per page | |
private int _bufferSize = 15; | |
// populate the Items member | |
public async Task UpdateLists(int page = 0) | |
{ | |
ObservableCollection<Item> items = await GetAllItems(); // or however you want to get your items | |
// skip page * bufferSize items and take bufferSize items | |
List<Item> newItems = items.Skip(page * _bufferSize).Take(_bufferSize)).ToList(); | |
if (page > 0) | |
{ | |
// append the new items to the end of the list | |
newItems.ForEach(item => | |
{ | |
Items.Add(item); | |
}); | |
} | |
else | |
{ | |
// replace all items in case it is the first page | |
Items = newItems; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment