Android:getView()position = 1中的convertView参数,当它应为null时始终不为null

时间:2016-06-06 13:05:41

标签: android gridview adapter

我有一个gridview来动态显示图片。当图片为2时,position = 1的convertview始终不为空?为什么? 我不问为什么getView()被多次调用。我的问题是为什么当第一次调用时position = 1总是不为null,而其他位置是null?

    <Gridview
        android:id="@+id/tgv_image_select"
        android:layout_width="270dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="@dimen/margin_10dp"
        android:gravity="center"
        android:horizontalSpacing="10dip"
        android:numColumns="4"
        android:verticalSpacing="10dip" />


  public class SelectImageGridViewAdapter extends BaseListAdapter<ImageItem> {

   private LoadFrescoImage loadFrescoImage;

public SelectImageGridViewAdapter(Context context) {

    super(context);
    loadFrescoImage = new LoadFrescoImage(Application.getInstance());
}

public void addImageItem(ImageItem imageItem) {
    if (imageItem == null) return;
    getData().add(imageItem);
    notifyDataSetChanged();
}

public void addImageItemLists(List<ImageItem> lists) {
    if(lists == null) return;
    getData().addAll(lists);
    notifyDataSetChanged();
}

public void removeItem(int position) {
    getData().remove(position);
    notifyDataSetChanged();
}

public void reloadImageItem(List<ImageItem> lists) {
    if (lists == null) return;
    setData(lists);
    notifyDataSetChanged();
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

    Holder holder;
    if (convertView == null) {
        holder = new Holder();
        convertView = mInflater.inflate(R.layout.grid_view_item, null);
        holder.mSelectImage = (ImageView)     
          convertView.findViewById(R.id.iv_icon);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }
    if (getData().size() > 0 && position != getData().size()) {
        holder.mSelectImage.setVisibility(View.VISIBLE);
        ImageItem imageItem = getData().get(position);
        if (imageItem != null) {

                String imguri = BitmapUtil.IMAGE_URI_SUFFIX_SDCARD + imageItem.getImageUri().getPath();
                Uri ImgUri = Uri.parse(imguri);
                loadFrescoImage.LoadImage(ImgUri,holder.mSelectImage);
            }
        }
    } else {
        if (position == Fragment.DEFAULT_PHOTO_COUNT) {
            holder.mSelectImage.setVisibility(View.GONE);
        } else {
            holder.mSelectImage.setVisibility(View.VISIBLE);
            holder.mSelectImage.setImageURI(BusinessUtil.getResourceUri(convertView.getContext(),R.drawable.guild_icon_img));
        }

    }

    return convertView;
}

class Holder {
    private ImageView mSelectImage;
}
  }



 when I choose 2 picture The log is :
    mytestnew   convertView = nullposition =0
    mytestnew   convertView = android.widget.FrameLayout{3f51f239 V.E.....      
        ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =0
                      ....
    mytestnew   convertView = android.widget.FrameLayout{3f51f239 V.E.....   
    ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =1

But,When I choose more than 2 pictures,the log is :
   mytestnew   convertView = nullposition =0
   mytestnew   convertView = android.widget.FrameLayout{3921e34b V.E.....       
        ........ 0,0-180,180 #7f0b0117 app:id/fl_item}position =0
                       ......   
   mytestnew   convertView = android.widget.FrameLayout{21daf777 V.E.....    
       ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =1
   mytestnew   convertView = null position =2
   mytestnew   convertView = null position =3

为什么position = 1始终不为null,而另一个位置为null?

1 个答案:

答案 0 :(得分:0)

参考this post了解详细的grideview回收机制。代码不应该关心converview。感谢@MikeM和@Karakuri