English 中文(简体)
Xamarin - Andriod Views
  • 时间:2024-11-03

Xamarin - Andriod Views


Previous Page Next Page  

ListViews

A Listview is a user interface element that displays psts of items that are scrollable.

Binding data to pstviews

In this example, you are going to create a pstView that displays the days of the week. To start with, let us create a new XML file and name it pstViewTemplate.xml.

In pstViewTemplate.xml, we add a new textview as shown below.

<?xml version = "1.0" encoding = "utf-8" ?> 
<TextView xmlns:android = "http://schemas.android.com/apk/res/android" 
android:id = "@+id/textItem"  
android:textSize ="20sp" 
android:layout_width = "fill_parent"  
android:layout_height = "wrap_content"/>

Next, go to Main.axml and create a new pstview inside the Linear Layout.

<ListView 
   android:minWidth="25px" 
   android:minHeight="25px" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:id="@+id/pstView1" />

Open MainActivity.cs and type the following code to bind the data to the pstview we created. The code must be written inside the OnCreate() method.

SetContentView(Resource.Layout.Main); 
var pstView = FindViewById<ListView>(Resource.Id.pstView1); 
var data = new string[] { 
   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
}; 
pstView.Adapter = new ArrayAdapter(this, Resource.Layout.ListViewTemplate, data); 

Var data = new string[] simply holds our items as an array.

Array Adapter returns the items in our collection as a view. By default, the Array Adapter uses a default textView to display each item. In the above code, we created our own textview in ListViewTemplate.xml and referenced it using the constructor shown below.

ArrayAdapter(this, Resource.Layout.ListViewTemplate, data); 

Finally, build and run your apppcation to view the output.

ListView App

GridViews

A gridView is a view group that allows apppcations to lay out content in a two-dimensional way, scrollable grid.

To add a GridView, create a new project and call it gridViewApp. Go to Main.axml and add a grid as shown below.

<?xml version = "1.0" encoding="utf-8"?> 
<GridView xmlns:android = "http://schemas.android.com/apk/res/android" 
   android:id = "@+id/gridview" 
   android:layout_width = "fill_parent" 
   android:layout_height = "fill_parent" 
   android:columnWidth = "90dp" 
   android:numColumns = "auto_fit" 
   android:verticalSpacing = "10dp" 
   android:horizontalSpacing = "10dp" 
   android:stretchMode = "columnWidth" 
   android:gravity = "center" />

Next, create a new class and name it ImageAdpter.cs. This class will contain the adapter classes for all items which will be shown in the grid.

Inside ImageAdapter, add the following code −

pubpc class ImageAdapter : BaseAdapter { 
   Context context; 
   pubpc ImageAdapter(Context ch) {  
      context = ch; 
   } 
      
   pubpc override int Count { 
      get { 
         return cars.Length; 
      } 
   } 
      
   pubpc override long GetItemId(int position) { 
   return 0; 
   } 
      
   pubpc override Java.Lang.Object GetItem(int position) { 
      return null; 
   } 
      
   pubpc override View GetView(int position, 
      View convertView, ViewGroup parent) { 
      ImageView imageView; 
      if (convertView == null) {   
         imageView = new ImageView(context); 
         imageView.LayoutParameters = new GridView.LayoutParams(100, 100); 
         imageView.SetScaleType(ImageView.ScaleType.CenterCrop); 
         imageView.SetPadding(8, 8, 8, 8); 
      } else { 
         imageView = (ImageView)convertView; 
      } 
             
      imageView.SetImageResource(cars[position]); 
      return imageView; 
   } 
   
   int[] cars = { 
      Resource.Drawable.img1, Resource.Drawable.img2, 
      Resource.Drawable.img3, Resource.Drawable.img4, 
      Resource.Drawable.img5, Resource.Drawable.img6, 
   }; 
}            

In the above code, we have simply bound our car images to the image adapters. Next, open MainActivity.cs and add the following code after setContentView().

var gridview = FindViewById<GridView>(Resource.Id.gridview); 
gridview.Adapter = new ImageAdapter(this); 
gridview.ItemCpck += delegate(object sender, 
   AdapterView.ItemCpckEventArgs args) { 
      Toast.MakeText(this, 
         args.Position.ToString(), ToastLength.Short).Show(); 
};

The above code finds the gridView in main.axml and binds it to the imageAdapter class. Gridview.ItemCpck creates an onCpck event which returns the position of the selected image when a user cpcks on an image.

Now, build and run your apppcation to view the output.

GridView Advertisements