1

我有自定义AdapterListView其中包含不同LayoutListView. 的选择Layout取决于getitemType我选择Layout文件。但是当我滚动这些项目时占据我的屏幕的前三个项目正在重复,直到我的列表大小但位置值发生变化。

     View getView(final int position, View convertView, ViewGroup parent)
    {
      case TYPE_MENUUPLOAD:
      case TYPE_UPLOADIMAGE: 
      ViewHolder holder4 = null;
      if (convertView == null) 
       {
            holder4 = new ViewHolder();
            convertView=Inflater.inflate(R.layout.uploadphoto,null);
            convertView.setTag(holder4);
        } else 
        { 
            holder4 = (ViewHolder)convertView.getTag();
        }       
      return convertView;

      case TYPE_USER:
          ViewHolder holder5 = null;     
          if (convertView == null) 
          { 
              holder5 = new ViewHolder();
              convertView = Inflater.inflate(R.layout.user, null);
              convertView.setTag(holder5); 
           } else 
           {
               holder5 = (ViewHolder)convertView.getTag();
            }
          return convertView; 

      case TYPE_UPLOADVIDEO:            
          ViewHolder holder6 = null;
          if (convertView == null) 
          {
              holder6 = new ViewHolder();
              convertView = mInflater.inflate(R.layout.uploadvideo, null);
              convertView.setTag(holder6);   
           } 
          else 
          {
              holder6 = (ViewHolder)convertView.getTag();
        }return convertView;

    };
4

2 回答 2

1
public int getViewTypeCount() {
   return 3; //in your case.
}

public int getItemViewType (int position) {
   case TYPE_MENUUPLOAD:
   case TYPE_UPLOADIMAGE:
      return 0;
   case TYPE_USER:
      return 1;
   case TYPE_UPLOADVIDEO:
      return 2;
}

您现有的 getView 方法应该无需任何更改即可工作。

于 2012-07-20T06:23:54.633 回答
0

在您的情况下,您需要删除if (convertView == null)支票。因为它导致了问题。

case TYPE_MENUUPLOAD: case TYPE_UPLOADIMAGE:你的视图膨胀......所以当case TYPE_USER:你有一个空检查时,convertView它不是nullinit in other case,所以你有错误的视图。

只需使用您的代码代码

  View getView(final int position, View convertView, ViewGroup parent)
{
  case TYPE_MENUUPLOAD:
  case TYPE_UPLOADIMAGE: 
  return Inflater.inflate(R.layout.uploadphoto,null);;
  case TYPE_USER:
  return Inflater.inflate(R.layout.user, null);; 
  case TYPE_UPLOADVIDEO:
  return mInflater.inflate(R.layout.uploadvideo, null);;
  }

View Holder在您的情况下,模式不会完全使用。

编辑: 我想到了 Prakash 建议的另一种解决方案。但是他的解决方案会以这种方式解决问题,如果确实如此..

View getView(final int position, View convertView, ViewGroup parent)
{
 ViewHolder holder = null;
 if (convertView == null) {
  case TYPE_MENUUPLOAD:
  case TYPE_UPLOADIMAGE:
        holder = new ViewHolder();
        convertView=Inflater.inflate(R.layout.uploadphoto,null);
        convertView.setTag(holder);
    break;
case TYPE_USER:
       holder = new ViewHolder();  
       convertView = Inflater.inflate(R.layout.user, null);
       convertView.setTag(holder); 
    break;
  case TYPE_UPLOADVIDEO:            
       holder = new ViewHolder();  
       convertView = mInflater.inflate(R.layout.uploadvideo, null);
       convertView.setTag(holder); 
    break;    
 } 
 else 
 {
    holder = (ViewHolder)convertView.getTag();
 }return convertView;

};
于 2012-07-20T06:00:44.177 回答