@Override protectedvoidonLayout(boolean changed, int l, int t, int r, int b){ if (mPicUrls == null || mPicUrls.size() == 0) return; for (int i = 0; i < getChildCount(); i++) { ItemImageView childView = (ItemImageView) getChildAt(i); int startX = getPaddingLeft(); int startY = getPaddingTop(); if (childView.getVisibility() == View.GONE) { continue; } if (i < mPicUrls.size()) { if (mPicUrls.size() == 1) { childView.layout(startX, startY, startX + childView.getMeasuredWidth(), startY + childView.getMeasuredHeight()); }elseif (mPicUrls.size() == 4) { int imageWidth = childView.getMeasuredWidth(); int line = i / 2; // 0 1 2 int column = i % 2;// 0 1 2 int left = startX + column * imageWidth + column * mSpaceWidth; int top = startY + line * imageWidth + line * mSpaceWidth; int right = left + imageWidth; int button = top + imageWidth; childView.layout(left, top, right, button); // LogUtil.d(this, "item image index = %s left = %s top = %s right = %s button = %s", // i, left, top, right, button); }else { int imageWidth = childView.getMeasuredWidth(); int line = i / 3; // 0 1 2 int column = i % 3;// 0 1 2 int left = startX + column * imageWidth + column * mSpaceWidth; int top = startY + line * imageWidth + line * mSpaceWidth; int right = left + imageWidth; int button = top + imageWidth; childView.layout(left, top, right, button); // LogUtil.d(this, "item image index = %s left = %s top = %s right = %s button = %s", // i, left, top, right, button); } } } }
for (int i = 0; i < getChildCount(); i++) { if (i < picUrls.size()) { getChildAt(i).setVisibility(VISIBLE); }else { getChildAt(i).setVisibility(GONE); } }