Skip to content

Adapter的泛型

L edited this page Mar 19, 2020 · 3 revisions

宗旨:GetView方法放在具体的Activity/Fragment里面实现,其他的均可以复用

/// <summary>
/// 通用适配器:新建GetViewEvent委托+OnGetView方法,在外部实现,在重写GetView方法中调用
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommonAdapter<T> : BaseAdapter<T>
{
	//要绑定的数据
	private readonly List<T> items;
	//页面上下文
	private readonly Activity context;
	//ListViewItem的布局id
	private readonly int layoutId;
	
	/// <summary>
	/// 委托
	/// </summary>
	public delegate View GetViewEvent(int position, View convertView, ViewGroup parent, T item, ViewHolder viewHolder);

	/// <summary>
	/// 加载item的View事件
	/// </summary>
	public event GetViewEvent OnGetView;

	public CommonAdapter(Activity context, int layoutId, List<T> items)
	{
		this.context = context;
		this.layoutId = layoutId;
		this.items = items;
	}

	public override T this[int position]
	{
		get { return items[position]; }
	}

	public override int Count
	{
		get { return items.Count; }
	}

	public override long GetItemId(int position)
	{
		return position;
	}

	public override View GetView(int position, View convertView, ViewGroup parent)
	{
		var item = items[position];
		var view = convertView;
		var viewHolder = ViewHolder.Get(context, convertView, parent, layoutId, position);

		if (OnGetView != null)
			return OnGetView(position, convertView, parent, item, viewHolder);
		return view;
	}
}
/// <summary>
/// 通用ViewHolder:保存控件,下拉刷新列表时只重新赋值,不再新建控件(节省内存)
/// Tag属性保证不错位
/// </summary>
public class ViewHolder : Object
{
	private readonly SparseArray<View> views;
	private int position;
	private readonly View ConvertView;

	public ViewHolder(Activity context, ViewGroup parent, int layoutID, int position)
	{
		this.position = position;
		views = new SparseArray<View>();
		ConvertView = context.LayoutInflater.Inflate(layoutID, null);
		ConvertView.Tag = this;
	}

	//单例模式获取对象实例
	public static ViewHolder Get(Activity context, View convertView, ViewGroup parent, int layoutID, int position)
	{
		if (convertView == null)
			return new ViewHolder(context, parent, layoutID, position);
		var holder = (ViewHolder) convertView.Tag;
		holder.position = position;
		return holder;
	}

	public View GetConvertView()
	{
		return ConvertView;
	}

	/// <summary>
	///  通过ViewID获取控件
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="viewId"></param>
	/// <returns></returns>
	public T GetView<T>(int viewId) where T : View
	{
		var view = views.Get(viewId);
		if (view == null)
		{
			view = ConvertView.FindViewById<T>(viewId);
			views.Put(viewId, view);
		}

		return (T) view;
	}
}

MainActivity

protected override void OnCreate(Bundle bundle)
{
	base.OnCreate(bundle);
	SetContentView(Resource.Layout.Main);
	ListView listView = FindViewById<ListView>(Resource.Id.id_lv_main);
	List<Student>  StudentsList =new List<Student>();
	StudentsList.Add(new Student()
	{
		Name = "朱泓霖"
	});
	CommonAdapter<Student > comAdapter =
		new CommonAdapter<Student>(this, Resource.Layout.Item, StudentsList);
	comAdapter.OnGetView += comAdapter_OnGetView;
	listView.Adapter = comAdapter;
}

private View comAdapter_OnGetView(int position, View convertView, ViewGroup parent, Student item, ViewHolder viewHolder)
{
	//通过ViewHolder获取对象并赋值
	viewHolder.GetView<TextView>(Resource.Id.id_tv_title).Text = item.Name;
	return viewHolder.GetConvertView();
}

参考资料

C# 泛型(Generic)

Clone this wiki locally