maandag 18 mei 2015

Creating a custom list in Xamarin.Android

Creating a custom list in Xamarin.Android is very straight forward and easy. First of all you need to place a listview component in an axml file where you want the list to show up.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
        <ListView
            android:id="@+id/left_drawer"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:choiceMode="singleChoice"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:paddingTop="20dp"
            android:background="#EAEAEA" />
</LinearLayout>


 After creating the Listview, The listview item itself can be made.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f0b444">
    <LinearLayout
        android:id="@+id/Text"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dip">
        <ImageView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center" />
        <TextView
            android:id="@+id/Text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="20sp"
            android:gravity="center"
            android:paddingLeft="16dp" />
    </LinearLayout>
</RelativeLayout>
 

In this example my customlist item is a little image with a textview next to it. After creating the listitem.axml file we can start defining our listitem. In this example I am calling the listitem an tableitem. So you should create a class with properties to define the values into it.

public class TableItem
    {
        public string Text { get; set; }
        public int Image { get; set; }
    }
 

After creating this TableItem we need to create a ListAdapter wich will eventually will fill the list.

public class ListAdapter : BaseAdapter<TableItem> {
        List<TableItem> items;
        Activity context;
        public ListAdapter(Activity context, List<TableItem> items)
            : base()
        {
            this.context = context;
            this.items = items;
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override TableItem this[int position]
        {
            get { return items[position]; }
        }
        public override int Count
        {
            get { return items.Count; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate(Resource.Layout.listitem, null);
            view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Text;
            view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.Image);
            return view;
        }
    }


After doing all these previous steps you are ready to create you mainActivity file.

[Activity (Label = "CustomList", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private ListView listView;
        List<TableItem> tableItems = new List<TableItem>();

        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            this.listView = this.FindViewById<ListView> (Resource.Id.left_drawer);

            // Get our button from the layout resource,
            // and attach an event to it
            tableItems.Add(new TableItem() { Text = "Map", Image = Resource.Drawable.Icon });
            tableItems.Add(new TableItem() { Text = "Settings", Image = Resource.Drawable.Icon });
            tableItems.Add(new TableItem() { Text = "Users", Image = Resource.Drawable.Icon });

            this.listView.Adapter = new ListAdapter(this, tableItems);

        }
 

The image is just the standard image wich can be found in the drawables map. But it can be any image you want.

The full code can be found at my Github: https://github.com/KClerix/CustomList-for-Xamarin.Android






Geen opmerkingen:

Een reactie posten