Using custom ListView items

Using a list view, we can entirely customize the way each item looks. We don't have to stick to a simple template, but rather provide a much more detailed view.

How to do it...

All we need to do in order to create a custom item template is to create a custom layout, either by code or in a layout resource:

  1. First, we create the item template using normal layout files.

    For example, we can create a layout named ListItemLayout.axml with three content views—an image view and two text views:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0" />
      <LinearLayout
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:id="@+id/firstRow" />
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:id="@+id/secondRow" />
      </LinearLayout>
    </LinearLayout>
  2. Assuming that we are going to use the SimpleAdapter type, we can construct our data in the following form:
    var data = new JavaList<IDictionary<string, object>>();
    data.Add(new JavaDictionary<string, object> {
      { "name", "Bruce Banner" },
      { "status", "Bruce Banner feels like SMASHING!" }
      { "icon", Resource.Drawable.hulk }
    });
  3. Now that we have our data, we can create the adapter, which will do all the work to update the view with the data:
    var adapter = new SimpleAdapter(
      this, data, Resource.Layout.ListItemLayout,
      new[] {
    "name",
    "status",
    "icon"
      }, new[] {
    Resource.Id.firstRow,
    Resource.Id.secondRow,
    Resource.Id.icon
      });

How it works...

Many apps will want to customize the list view in order to have more complex items as well as to continue using the same theme throughout the app. In order to achieve this, we create custom items for the list view and style the list view itself. For example, consider the inbox of the Gmail app or the results of the Google Now app; each is a list view, but each item has been greatly customized and has many data points.

Custom list view items are simply normal views, such as an inflated layout, that are repeated for each item. Each list view item can contain any view or any view structure, just like in an activity layout. Although this is the case, we need to keep in mind that the item view is in a scrollable list; thus, we shouldn't put another scrollable view inside a list view item.

Another thing we should remember is that the item view will be repeated many times, so we should not create an overly complex view. We should strive to keep our list items as shallow as possible to increase performance, especially as there will be many items and they will be moving on the screen when the user scrolls.

Tip

The ListView items should be kept as shallow as possible to increase performance.

There's more...

If we want to specify an actual custom type, such as from a SQLite.NET data type, then we can create an instance of the BaseAdapter type. This provides a clean way to map any datatype to any view with full control over each item. The SimpleAdapter type tries to handle several view types, but is not designed for very complex items.

See also

  • The Using a BaseAdapter with arbitrary data recipe
  • The Optimizing the ListView recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.161.77