Optimizing the ListView

In order to make scrolling through out lists as smooth as possible, we need to ensure that the construction and manipulation of the items run as fast and as efficiently as possible.

How to do it...

One way in which we can optimize the list items is to reuse the item view:

  • This is very easy to do, and all we need is to check is whether we can use the convertView parameter before trying to create a new instance of the item:
    if (convertView == null) {
      var inflater = LayoutInflater.From(context);
      convertView = inflater.Inflate(
        Resource.Layout.ListItemLayout, parent, false);
    }
    var firstRow = convertView.FindViewById<TextView>(
      Resource.Id.firstRow);
    firstRow.Text = person.Name;

Another way is to store references to the various subviews ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.