扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
小编给大家分享一下xamarin Android如何实现ListView万能适配器,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
为老边等地区用户提供了全套网页设计制作服务,及老边网站建设行业解决方案。主营业务为网站制作、成都网站建设、老边网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!本章主要分为以下三点:
打造通用的ViewHolder优化ListView性能
使用泛型Adapter适应不同布局的ListView
C#委托和Java匿名内部类的比较
打造通用的ViewHolder优化ListView性能
public class ViewHolder:Java.Lang.Object { private SparseArrayViews; View ConvertView; private Context context ; int mPosition; private ViewHolder(Context _context,ViewGroup parent ,int itemLayoutId,int position) { this.mPosition = position; Views = new SparseArray (); ConvertView = LayoutInflater.From(_context).Inflate(itemLayoutId,null); ConvertView.Tag = this; } public static ViewHolder Get(Context context , View convertView,ViewGroup parent ,int itemLayoutId,int position) { if (convertView == null) { return new ViewHolder(context, parent, itemLayoutId, position); } else { ViewHolder holder = (ViewHolder)convertView.Tag; holder.mPosition = position; return holder; } } public T GetView (int viewId) where T :View { View view = Views.Get(viewId); if (view == null) { view = ConvertView.FindViewById (viewId); Views.Put(viewId,view); } return (T)view; } public View GetConvertView() { return ConvertView; } /// /// 给TextView 设置文本 /// /// /// ///public ViewHolder SetText(int viewId ,string text) { TextView view = GetView (viewId); view.Text = text; return this; } /// /// 给ImageView 设置图片 /// public ViewHolder SetImageBitMap(int viewId , Bitmap bm) { ImageView view = GetView(viewId); view.SetImageBitmap(bm); return this; } }
稍微解释一下:
ViewHolder的构造方法中初始化变量,并将ListView的布局view的Tag设置成viewholder的实例
Get方法使用单例模式初始化ViewHolder
GetView泛型方法获取ListView布局中的空间
SetText、SetImageBitMap给一些常用的控件复值
使用泛型Adapter适应不同布局的ListView
CommonAdapter是一个泛型的基类,一些重复的重写方法都在这个基类,然后我们写一个子类继承这个CommonAdapter,就简化了很多代码
public abstract class CommonAdapter:BaseAdapter { Context mContext; List mData; int mItemLayoutId; public CommonAdapter(Context context, List data, int itemLayoutId):base() { this.mContext = context; mData = data; mItemLayoutId = itemLayoutId; } public override int Count { get { return mData.Count; } } public override Java.Lang.Object GetItem(int position) { return null; } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { var item = mData[position]; ViewHolder viewHolder = ViewHolder.Get(mContext, convertView, parent, mItemLayoutId, position); convert(viewHolder,mData[position]); System.Diagnostics.Debug.Write(position); return viewHolder.GetConvertView(); } public abstract void convert(ViewHolder helper, T item); public ViewHolder GetViewHolder(int position, View convertView, ViewGroup parent) { return ViewHolder.Get(mContext, convertView, parent, mItemLayoutId, position); } }
写一个newsAdapter继承CommonAdapter,只需要将赋值的代码写在重写方法convert里面极客,这样就简化了很多的代码
public class NewsAdapter: CommonAdapter { public NewsAdapter(Context context, List data, int resId) : base(context, data, resId) { } public override void convert(ViewHolder helper, T item) { NewsViewModel model = (NewsViewModel)Convert.ChangeType(item, typeof(NewsViewModel)); helper.SetText(Resource.Id.tv_news_title, model.Title); helper.SetText(Resource.Id.tv_news_id, model.NewsID.ToString()); helper.SetText(Resource.Id.tv_news_desc, model.Desc); } }
C#委托和Java匿名内部类的比较
看来上面的例子,虽然简化了很多代码,但是每个ListView还是得新建一个Adapter,看来hongyang大神的博客在java中用匿名内部类实现一个Adapter万能通用,但是c#中没有匿名内部内的概念,这就尴尬了。。。。。
c#中虽然没有这种概念,但语言是相通的,但是委托可以实现。万能通用的adapter代码如下:
public class Common1Adapter: BaseAdapter { Context mContext; List mData; int mItemLayoutId; public delegate View GetViewEvent(int position, View convertView, ViewGroup parent, T item, ViewHolder viewHolder); public event GetViewEvent OnGetView; public Common1Adapter(Context context, List data, int itemLayoutId) : base() { this.mContext = context; mData = data; mItemLayoutId = itemLayoutId; } public override int Count { get { return mData.Count; } } public override Java.Lang.Object GetItem(int position) { return null; } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { var item = mData[position]; ViewHolder viewHolder = ViewHolder.Get(mContext, convertView, parent, mItemLayoutId, position); if (OnGetView != null) return this.OnGetView(position,convertView,parent,item,viewHolder); return convertView; } } }
在使用时,只需要在Activity中注册OnGetView 事件,这样多个ListView的Adapter就只需要一个通用的Adapter就行了
View OnGetView(int position,View convertView,ViewGroup parent,NewsViewModel model,ViewHolder viewHolder) { viewHolder.SetText(Resource.Id.tv_news_title, model.Title); viewHolder.SetText(Resource.Id.tv_news_id, model.NewsID.ToString()); viewHolder.SetText(Resource.Id.tv_news_desc, model.Desc); return viewHolder.GetConvertView(); } Listlist_news = new List () { new Adaptes.NewsViewModel () { NewsID =1,Title="测试标题124578cdascdas",Desc ="测acdsdas试内内容10c2da4s内容10c2da4s内容10c2da4s容10c2da4sc5das4cdas"}, new Adaptes.NewsViewModel () { NewsID =2,Title="cascascda24578cdascdas",Desc ="测dasc试内容10c2da4s内容10c2da4s内容10c2da4s内容10c2da4sc5das4cdas"}, }; Common1Adapter adapter = new Adaptes.Common1Adapter (this, list_news, Resource.Layout.item_listview_news); adapter.OnGetView += OnGetView; lv_news.Adapter = adapter;
以上是“xamarin Android如何实现ListView万能适配器”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流