mono for Android ListView

ListView控件使用

1.ListView是一个列表布局控件,在列表中显示简单的数据:

新建一个解决方案,配置main.axml的文件:

View Code
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2             android:id="@+id/Ll_SC"
 3             android:layout_width="fill_parent"
 4             android:layout_height="fill_parent"
 5              android:background="#ff909090"
 6             android:layout_margin="5dip"
 7             >
 8             <ListView
 9                 android:id="@+id/Lv_SC"
10                 android:layout_width="fill_parent"
11                 android:layout_height="fill_parent"            android:cacheColorHint="#00000000"/>    
12        </LinearLayout>

 C#后台代码content.cs

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using Android.App;
 6 using Android.Content;
 7 using Android.OS;
 8 using Android.Runtime;
 9 using Android.Views;
10 using Android.Widget;
11 namespace LV{
12     [Activity(Label = "Content", MainLauncher = true, Icon = "@drawable/icon")]
13     public class content : Activity
14     {
15 protected override void OnCreate(Bundle bundle)
16         {
17             base.OnCreate(bundle);
18             SetContentView(Resource.Layout.Main);
19 ListView Lv_SC = FindViewById<ListView>(Resource.Id.Lv_SC);
20 Lv_SC.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);}
21  static readonly string[] list = new string[] { "baidu", "xinlang", "guge","jingdong","taobao","58","CSDN","fanke","sougou"};}

2.使用自定义列表适配器显示数据:

添加一个布局文件ListItemView.axml,此文件就是Lv_SC控件中行的布局

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="60dip"
 5     android:stretchColumns="1">
 6     <TableRow>
 7         <ImageView
 8             android:id="@+id/Image"
 9             android:layout_width="60dip"
10             android:layout_height="fill_parent"
11             android:layout_gravity="center_vertical" />
12         <LinearLayout
13             android:layout_width="wrap_content"
14             android:layout_height="fill_parent"
15             android:orientation="vertical"
16             android:layout_marginLeft="10px"
17             android:layout_marginTop="10px">
18             <TextView
19                 android:id="@+id/Text1"
20                 android:layout_width="wrap_content"
21                 android:layout_height="wrap_content" />
22             <TextView
23                 android:id="@+id/Text2"
24                 android:layout_width="wrap_content"
25                 android:layout_height="wrap_content" />
26         </LinearLayout>
27           </TableRow>
28 </TableLayout>

 C#后台代码content.cs

  1 using System;
  2   using System.Collections.Generic;
  3   using System.Linq;
  4   using System.Text;
  5    using Android.App;
  6   using Android.Content;
  7    using Android.OS;
  8   using Android.Runtime;
  9   using Android.Views;
 10  using Android.Widget;
 11  using Android.Util;
 12  using Java.Util;
 13  using Login;
 14  namespace Login
 15  {
 16  [Activity(Label = "Content", MainLauncher = true, Icon = "@drawable/icon")]
 17  public class content : Activity
 18  {       
 19  protected override void OnCreate(Bundle bundle)
 20  {
 21  base.OnCreate(bundle);
 22  SetContentView(Resource.Layout.Main);
 23  LinearLayout Ll_SC = FindViewById<LinearLayout>(Resource.Id.Ll_SC);
 24            //Lv_SC.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
 25             Lv_SC.Adapter =new CustomListAdapter(this);
 26 //添加ListItem事件
 27  //Lv_SC.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(Lv_Click);
 28  }
 29  }
 30  // 设置一个数据对象
 31  public class ScClass
 32  {
 33  public string Name
 34  {
 35  get;
 36  set;
 37  }
 38  public string Description
 39  {
 40  get;
 41  set;
 42  }
 43  public int Image
 44  {
 45  get;
 46  set;
 47  }
 48  }
 49  //ListView自定义Adater
 50  public class CustomListAdapter : BaseAdapter
 51  {
 52  Activity context;
 53  public List<ScClass> items;
 54  public CustomListAdapter(Activity context) 
 55  : base()
 56  {
 57  this.context = context;
 58  //初始化数据
 59  this.items = new List<ScClass>() {
 60  new ScClass() { Name = "beijing", Description = "北京北京北京", Image = Resource.Drawable.Image1 },
 61  new ScClass() { Name = "上shanghai", Description = "上海上海上海", Image = Resource.Drawable.Image2 },
 62  new ScClass() { Name = "jiangxi", Description = "江西江西江西", Image = Resource.Drawable.Image3 },
 63  new ScClass() { Name = "wuhan", Description = "武汉武汉武汉", Image = Resource.Drawable.Image4 }, 
 64  };
 65  }
 66  //返回ListItem行数
 67           public override int Count
 68  {
 69  get { return items.Count; }
 70  }
 71  public override Java.Lang.Object GetItem(int position)
 72  {
 73  return position;
 74  }
 75  public override long GetItemId(int position)
 76  {
 77  return position;
 78  }
 79  public override View GetView(int position, View convertView, ViewGroup parent)
 80  {
 81  var item = items[position];         
 82  var view = (convertView ?? 
 83  context.LayoutInflater.Inflate(
 84  Resource.Layout.ListItemView, 
 85  parent, 
 86  false)) as LinearLayout;
 87  var imageItem = view.FindViewById(Resource.Id.Image) as ImageView;
 88  var textTop = view.FindViewById(Resource.Id.Text1) as TextView;
 89  var textBottom = view.FindViewById(Resource.Id.Text2) as TextView;
 90  imageItem.SetImageResource(item.Image);
 91  textTop.SetText(item.Name, TextView.BufferType.Normal);
 92  textBottom.SetText(item.Description, TextView.BufferType.Normal);
 93  return view;
 94  }
 95  public ScClass GetItemAtPosition(int position)
 96  {
 97  return items[position];
 98  }
 99  
100  }
101 }

注:

当在listview的item中嵌入了checkbox, webview等事件优先级高的控件时,listview的onItemClick事件会被屏蔽无响应。。。

这时我们可以通过将item中控件的focuse设为false来解决问题。

使用静态方法:在布局文件中添加 android:focusable="false" 

使用动态方法:在代码中调用 XXXView.setFocusable(false)

 

posted on 2013-03-13 13:37  Feel  阅读(360)  评论(0)    收藏  举报

导航