我目前正在开发一个非常简单的 ListView,最多有 2 种不同的视图类型。为了使一切顺利,我尝试回收视图。我的代码现在是:
@Override
public int getItemViewType (int position){
if(mHasBefore&&position==0){
return TYPE_PAGER;
}else if(mHasNext&&position==getCount()-1){
return TYPE_PAGER;
}else return TYPE_SCORE;
}
@Override
public int getViewTypeCount (){
return 1+((mHasBefore==true||mHasNext==true)?1:0);
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("outa","View: pos: "+position+ " . "+getItemViewType(position)+" - "+getViewTypeCount());
if(getItemViewType(position)==TYPE_SCORE)return getScoreView(position,convertView,parent);
else return getPagerView(position,convertView,parent);
}
protected View getPagerView(int position,View convertView,ViewGroup parent){
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem_highscore_pager, null);}}
protected View getScoreView(int position,View convertView,ViewGroup parent){
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem_highscore, null);}}
基本上它应该在最后一项上显示不同的视图,但即使 LogCat 在 getView() 中声明它不是 TYPE_SCORE,它仍然显示默认项。因此 convertView 确实提供了一个回收的视图。我做错了什么还是这是正常行为?