Reputation: 117
I have a ListView that is populated by a List of News objects, which are retrieved from a service that downloads an rss feed from a website and converts the data into news objects. Each news object contains a pubdate, title, link, and description. My custom viewcell simply displays the date in a box on the left, then the title across the space remaining on the right, with space between each item. The problem is that the listview only displays the first 17 of 70+ items, after number 17 the rest of the viewcells are there but they are empty, no text or box for the date. I also have pull to refresh enabled, which just calls the service again to repopulate the list (inefficient I know, but also working on a way to only update the list with newly added news items) and when I pull to refresh, the entire listview becomes blank viewcells.
So far I've tried a few things that didn't lead towards a solution, they only made me even more confused. I've tried to limit the number of items added by doing a Take(20) on the list, which displays the first 16 items, then 2 blank items, then 1 correctly displayed item, followed by more blanks. When this shorter list is refreshed and called again, all cells except the 14th are blank. It appears random which ones, if any, stay there after a refresh.
To confirm something though, the items are still clickable. I can still tap on any item, even if blank, and it will open the browser to the correct article.
<ListView
x:Name="newsList"
Margin="0"
BackgroundColor="Transparent"
HasUnevenRows="True"
HorizontalOptions="CenterAndExpand"
IsPullToRefreshEnabled="True"
ItemSelected="NewsList_ItemSelected"
Refreshing="NewsList_Refreshing"
SeparatorVisibility="None"
VerticalOptions="CenterAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame
Margin="0,5,0,5"
Padding="2"
BorderColor="Gray"
HeightRequest="60">
<AbsoluteLayout Padding="0">
<StackLayout
AbsoluteLayout.LayoutBounds="0, 0, 60, 60"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="Gray">
<Label
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
Text="{Binding Date, StringFormat='{0:MMM d}'}"
TextColor="White"
VerticalOptions="CenterAndExpand" />
<Label
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
Text="{Binding Date, StringFormat='{0:yyyy}'}"
TextColor="White"
VerticalOptions="CenterAndExpand" />
</StackLayout>
<Label
Margin="5"
AbsoluteLayout.LayoutBounds="60, 0, .85, 1"
AbsoluteLayout.LayoutFlags="SizeProportional"
BackgroundColor="White"
FontSize="16"
Label.MaxLines="2"
LineBreakMode="WordWrap"
Text="{Binding Title}" />
</AbsoluteLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and the code behind
private NewsService newsService = new NewsService();
public NewsPage()
{
InitializeComponent();
newsList.ItemsSource = newsService.ParseRssFile().Take(20);
Application.Current.Properties["newsUpdated"] = DateTime.Today;
}
private void NewsList_Refreshing(object sender, System.EventArgs e)
{
newsList.ItemsSource = newsService.ParseRssFile();
newsList.EndRefresh();
}
private void NewsList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var News = e.SelectedItem as News;
//do something...
var uri = new Uri(News.Link);
Device.OpenUri(uri);
}
The news service just returns a List of News objects, each containing a string for title, link, description, and a DateTime for date.
Expected result is correctly displayed news dates + titles, I assume around 70 items shouldn't be difficult to display.
EDIT: Tried to print each News item's Title and pubdate in debug to confirm they weren't all actually blank somehow, they aren't blank.
Upvotes: 0
Views: 785
Reputation: 117
I think I've found the solution to this problem, I enabled a listview tag:
CachingStrategy="RecycleElementAndDataTemplate"
And now all viewcells are populated correctly. I don't know why it works, but it does. If anyone can offer insight that'd be appreciated!
Upvotes: 0