Creating an XML interface and ListView
Our starting point is the main.xml
sheet; this is where we will be creating the ListView
:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="10dp" android:layout_marginTop="5dp" android:background="@android:color/transparent" android:cacheColorHint="@android:color/transparent" android:divider="#CCCCCC" android:dividerHeight="1dp" android:paddingLeft="2dp" /> </LinearLayout>
Tip
The main.xml
file should already be in the resource | layout directory, so simply copy and paste the previous code into this file.
Excellent! We now have our starting activity and interface, so now we have to create a ListAdapter
for our ListView
. An adapter works very much like a UITableSource
, where we must override functions to determine cell data, row design, and the number of items in the list.
Note
Xamarin Studio also has an Android GUI designer.
Right-click on the Android project and add a new empty class file for our adapter class. Our class must inherit the BaseAdapter
class, and we are going to override the following functions:
public override long GetItemId(int position); public override View GetView(int position, View convertView, ViewGroup parent);
Before we go any further, we need to create a model for the objects used to contain the data to be presented in each row. In our iOS project, we created a GalleryItem
to hold the byte array of image data used to create each UIImage
. We have two approaches here: we could create another object to do the same as the GalleryItem
, or even better, why don't we reuse this object using a shared project?