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.
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:
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>
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 } });
var adapter = new SimpleAdapter( this, data, Resource.Layout.ListItemLayout, new[] { "name", "status", "icon" }, new[] { Resource.Id.firstRow, Resource.Id.secondRow, Resource.Id.icon });
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.
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.