我爲具有兩種類型的行的ListView創建了一個適配器。 ListView有4行。最後一行有不同的佈局,這就是爲什麼我在getview中使用GetItemViewType方法的原因瞭解ViewHolder模式
我想了解該模式如何工作。我看着這樣的: https://www.youtube.com/watch?v=bWsWe9T9HJw,以獲得更好的理解 回收是如何工作的
什麼,我不明白的是:當我在我的列表視圖向下滾動,在convertview 總是空。當我再次滾動備份時,convertview不是null並且是可重用的。
不應該convertview爲null只爲列表中的第一個項目?我不明白爲什麼 它爲每個新項目爲空?只有在列表中的第一項
public override View GetView (int position, View convertView, ViewGroup parent)
{
BaseBundelVO bundle = _bundles [position];
DSBundleListItem bundleHolder = null;
DSBundleArchiveItem archiveHolder = null;
int type = GetItemViewType(position);
if (convertView == null)
{
bundleHolder = new DSBundleListItem (_activity);
archiveHolder = new DSBundleArchiveItem (_activity);
switch (type)
{
case 0:
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.dsBundleListItem, null);
bundleHolder.IconIv = convertView.FindViewById<ImageView> (Resource.Id.iconIv);
bundleHolder.CoverIv = convertView.FindViewById<ImageView> (Resource.Id.coverIv);
bundleHolder.CoverTitleTv = convertView.FindViewById<TextView> (Resource.Id.coverTitleTv);
bundleHolder.CoverSubTitleTv = convertView.FindViewById<TextView> (Resource.Id.coverSubTitleTv);
bundleHolder.BundleProgress = convertView.FindViewById<ProgressBar> (Resource.Id.bundleProgress);
convertView.Tag = bundleHolder;
break;
case 1:
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.dsBundleArchiveItem, null);
archiveHolder.ArchiveTitleTv = convertView.FindViewById<TextView> (Resource.Id.archiveTitleTv);
archiveHolder.ArchiveSubTitleTv = convertView.FindViewById<TextView> (Resource.Id.archiveSubTitleTv);
convertView.Tag = archiveHolder;
break;
}
}
else
{
switch (type)
{
case 0:
bundleHolder = (DSBundleListItem)convertView.Tag;
Console.WriteLine (bundleHolder.IsDisposed());
bundleHolder.RemoveImageLoaderCallBack();
break;
case 1:
archiveHolder = (DSBundleArchiveItem)convertView.Tag;
Console.WriteLine (archiveHolder.IsDisposed());
archiveHolder.RemoveImageLoaderCallBack();
break;
}
}
switch (type)
{
case 0:
bundleHolder.CoverTitleTv.Text = bundle.Title;
bundleHolder.CoverSubTitleTv.Text = bundle.SubTitle;
bundleHolder.LoadImage(bundle.CoverImageLocation,bundle.Icon);
break;
case 1:
archiveHolder.ArchiveTitleTv.Text = "Archief";
archiveHolder.ArchiveSubTitleTv.Text = "Bekijk onze eerder verschenen publicaties";
break;
}
return convertView;
}
如果你的屏幕可以適合說2個視圖,那麼android會加載視圖,2 visibile視圖,並在底部的下一個視圖,所以它至少需要4個views.This解釋了爲什麼它不只是首先convertview null。順便說一句,你應該檢查與Api 21新的RecyclerView。 –