Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.ViewGroup;
- public class DashboardWithSquaredCells extends ViewGroup {
- private int mRows;
- private int mColumns;
- private int mDividerOffset;
- public DashboardWithSquaredCells(Context context) {
- super(context);
- init(context, null);
- }
- public DashboardWithSquaredCells(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- init(context, attrs);
- }
- public DashboardWithSquaredCells(Context context, @Nullable AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(context, attrs);
- }
- private void init(final @NonNull Context context, final @Nullable AttributeSet attrs) {
- final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.DashboardWithSquaredCells);
- try {
- mRows = Math.max(styledAttributes.getInt(R.styleable.DashboardWithSquaredCells_rows, 1), 1);
- mColumns = Math.max(styledAttributes.getInt(R.styleable.DashboardWithSquaredCells_columns, 1), 1);
- mDividerOffset = Math.max(styledAttributes.getDimensionPixelSize(R.styleable.DashboardWithSquaredCells_divider_offset, 0), 0);
- } finally {
- styledAttributes.recycle();
- }
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- final int childSize = Math.max(0, (getMeasuredWidth() - mDividerOffset * (mColumns + 1)) / mColumns);
- final int childSizeSpec = MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.AT_MOST);
- int visibleChilds = 0;
- for (int childIndex = 0, count = getChildCount(); childIndex < count; ++ childIndex) {
- final View child = getChildAt(childIndex);
- if (View.GONE != child.getVisibility()) {
- child.measure(childSizeSpec, childSizeSpec);
- ++ visibleChilds;
- }
- }
- final int visibleRows = (visibleChilds / mColumns) + ((visibleChilds % mColumns) > 0 ? 1 : 0);
- final int manuallyMeasuredHeight = childSize * visibleRows + (visibleRows + 1) * mDividerOffset;
- setMeasuredDimension(getMeasuredWidth(), manuallyMeasuredHeight);
- }
- @SuppressWarnings("ResourceType")
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- final int availableWidth = r - l;
- final int availableHeight = b - t;
- final int cellWidth = Math.max(0, (availableWidth - mDividerOffset * (mColumns + 1)) / mColumns);
- final int cellHeight = Math.max(0, (availableHeight - mDividerOffset * (mRows + 1)) / mRows);
- int visibleChildIndex = 0;
- for (int childIndex = 0, count = getChildCount(); childIndex < count; childIndex++) {
- final View child = getChildAt(childIndex);
- if (View.GONE == child.getVisibility()) {
- continue;
- }
- final int rowIndex = visibleChildIndex / mColumns;
- final int columnIndex = visibleChildIndex % mColumns;
- final int currentLeft = mDividerOffset * (columnIndex + 1) + cellWidth * columnIndex;
- final int currentTop = mDividerOffset * (rowIndex + 1) + cellHeight * rowIndex;
- final int currentRight = (0 == mDividerOffset && columnIndex == mColumns - 1) ? r : (currentLeft + cellWidth);
- final int currentBottom = (0 == mDividerOffset && rowIndex == mRows - 1) ? b : (currentTop + cellHeight);
- child.layout(currentLeft, currentTop, currentRight, currentBottom);
- ++ visibleChildIndex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement