public class CustomAdapter : BaseAdapter{
private const int TYPE_ITEM = 0;
private const int TYPE_SEPARATOR = 1;
private List<String> mData;
private TreeSet sectionHeader;
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.GetSystemService(Context.LayoutInflaterService);
}
public void addItem( String item) {
mData.Add(item);
NotifyDataSetChanged();
}
public void addSectionHeaderItem(String item) {
mData.Add(item);
sectionHeader.Add(mData.Count - 1);
NotifyDataSetChanged();
}
public int getItemViewType(int position) {
return sectionHeader.Contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
public int getViewTypeCount {
get{ return 2; }
}
public override int Count {
get {return mData.Count;}
}
public override GetItem this[int position] {
get{ return mData [position]; }
}
public override long GetItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.Inflate(Resource.Layout.textViewItemsSeparator, parent);
holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewHeaderItems);
break;
case TYPE_SEPARATOR:
convertView = mInflater.Inflate(Resource.Layout.textViewHeaderItems, parent);
holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewItemsSeparator);
break;
}
convertView.Tag=holder;
} else {
holder = (ViewHolder)convertView.Tag as ViewHolder;
}
holder.textView.Text=mData[position];
return convertView;
}
public class ViewHolder:Java.Lang.Object {
public TextView textView;
}
&#13;
OnActivityCreated:
mAdapter=new CustomAdapter(this);
for (int i = 0; i <= items.Count; i++) {
mAdapter.addItem (items [i].ToString ());
if (i == 0) {
mAdapter.addSectionHeaderItem ("Music");
} else if(i==13) {
mAdapter.addSectionHeaderItem ("Audio");
}
}
lst.Adapter = new CustomAdapter (Activity, mAdapter);
&#13;
为ListView创建自定义适配器
您必须覆盖getViewTypeCount()方法。此方法返回将由getView方法创建的视图类型的数量。
我正在尝试创建标题列表视图。
答案 0 :(得分:1)
使用TextView
holder.textView.Text = mData[position];
在幕后,Xamarin将 C# string
转换为 Java CharSequence
并为您调用setText
。
<小时/> P.S
您必须查看文档!当你是初学者时,这是必须的。