PseudoGridViewpublic class PseudoGridView extends android.view.ViewGroup A view that arranges it's children in a grid with a fixed number of evenly spaced columns.
{@see android.widget.GridView} |
Fields Summary |
---|
private int | mNumColumns | private int | mVerticalSpacing | private int | mHorizontalSpacing |
Constructors Summary |
---|
public PseudoGridView(android.content.Context context, android.util.AttributeSet attrs)
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PseudoGridView);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.PseudoGridView_numColumns:
mNumColumns = a.getInt(attr, 3);
break;
case R.styleable.PseudoGridView_verticalSpacing:
mVerticalSpacing = a.getDimensionPixelSize(attr, 0);
break;
case R.styleable.PseudoGridView_horizontalSpacing:
mHorizontalSpacing = a.getDimensionPixelSize(attr, 0);
break;
}
}
a.recycle();
|
Methods Summary |
---|
protected void | onLayout(boolean changed, int l, int t, int r, int b)
boolean isRtl = isLayoutRtl();
int children = getChildCount();
int rows = (children + mNumColumns - 1) / mNumColumns;
int y = 0;
for (int row = 0; row < rows; row++) {
int x = isRtl ? getWidth() : 0;
int maxHeight = 0;
int startOfRow = row * mNumColumns;
int endOfRow = Math.min(startOfRow + mNumColumns, children);
for (int i = startOfRow; i < endOfRow; i++) {
View child = getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
if (isRtl) {
x -= width;
}
child.layout(x, y, x + width, y + height);
maxHeight = Math.max(maxHeight, height);
if (isRtl) {
x -= mHorizontalSpacing;
} else {
x += width + mHorizontalSpacing;
}
}
y += maxHeight;
if (row > 0) {
y += mVerticalSpacing;
}
}
| protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
throw new UnsupportedOperationException("Needs a maximum width");
}
int width = MeasureSpec.getSize(widthMeasureSpec);
int childWidth = (width - (mNumColumns - 1) * mHorizontalSpacing) / mNumColumns;
int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
int childHeightSpec = MeasureSpec.UNSPECIFIED;
int totalHeight = 0;
int children = getChildCount();
int rows = (children + mNumColumns - 1) / mNumColumns;
for (int row = 0; row < rows; row++) {
int startOfRow = row * mNumColumns;
int endOfRow = Math.min(startOfRow + mNumColumns, children);
int maxHeight = 0;
for (int i = startOfRow; i < endOfRow; i++) {
View child = getChildAt(i);
child.measure(childWidthSpec, childHeightSpec);
maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
}
int maxHeightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY);
for (int i = startOfRow; i < endOfRow; i++) {
View child = getChildAt(i);
if (child.getMeasuredHeight() != maxHeight) {
child.measure(childWidthSpec, maxHeightSpec);
}
}
totalHeight += maxHeight;
if (row > 0) {
totalHeight += mVerticalSpacing;
}
}
setMeasuredDimension(width, getDefaultSize(totalHeight, heightMeasureSpec));
|
|