小編給大家分享一下xamarin Android如何實現(xiàn)ListView萬能適配器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)專注于武漢企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,電子商務(wù)商城網(wǎng)站建設(shè)。武漢網(wǎng)站建設(shè)公司,為武漢等地區(qū)提供建站服務(wù)。全流程按需定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
本章主要分為以下三點:
打造通用的ViewHolder優(yōu)化ListView性能
使用泛型Adapter適應(yīng)不同布局的ListView
C#委托和Java匿名內(nèi)部類的比較
打造通用的ViewHolder優(yōu)化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 設(shè)置文本 /// /// /// ///public ViewHolder SetText(int viewId ,string text) { TextView view = GetView (viewId); view.Text = text; return this; } /// /// 給ImageView 設(shè)置圖片 /// public ViewHolder SetImageBitMap(int viewId , Bitmap bm) { ImageView view = GetView(viewId); view.SetImageBitmap(bm); return this; } }
稍微解釋一下:
ViewHolder的構(gòu)造方法中初始化變量,并將ListView的布局view的Tag設(shè)置成viewholder的實例
Get方法使用單例模式初始化ViewHolder
GetView泛型方法獲取ListView布局中的空間
SetText、SetImageBitMap給一些常用的控件復(fù)值
使用泛型Adapter適應(yīng)不同布局的ListView
CommonAdapter是一個泛型的基類,一些重復(fù)的重寫方法都在這個基類,然后我們寫一個子類繼承這個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匿名內(nèi)部類的比較
看來上面的例子,雖然簡化了很多代碼,但是每個ListView還是得新建一個Adapter,看來hongyang大神的博客在java中用匿名內(nèi)部類實現(xiàn)一個Adapter萬能通用,但是c#中沒有匿名內(nèi)部內(nèi)的概念,這就尷尬了。。。。。
c#中雖然沒有這種概念,但語言是相通的,但是委托可以實現(xiàn)。萬能通用的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中注冊O(shè)nGetView 事件,這樣多個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="測試標(biāo)題124578cdascdas",Desc ="測acdsdas試內(nèi)內(nèi)容10c2da4s內(nèi)容10c2da4s內(nèi)容10c2da4s容10c2da4sc5das4cdas"}, new Adaptes.NewsViewModel () { NewsID =2,Title="cascascda24578cdascdas",Desc ="測dasc試內(nèi)容10c2da4s內(nèi)容10c2da4s內(nèi)容10c2da4s內(nèi)容10c2da4sc5das4cdas"}, }; Common1Adapter adapter = new Adaptes.Common1Adapter (this, list_news, Resource.Layout.item_listview_news); adapter.OnGetView += OnGetView; lv_news.Adapter = adapter;
以上是“xamarin Android如何實現(xiàn)ListView萬能適配器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!