Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. public class DashboardWithSquaredCells extends ViewGroup {
  10.  
  11. private int mRows;
  12. private int mColumns;
  13. private int mDividerOffset;
  14.  
  15. public DashboardWithSquaredCells(Context context) {
  16. super(context);
  17. init(context, null);
  18. }
  19.  
  20. public DashboardWithSquaredCells(Context context, @Nullable AttributeSet attrs) {
  21. super(context, attrs);
  22. init(context, attrs);
  23. }
  24.  
  25. public DashboardWithSquaredCells(Context context, @Nullable AttributeSet attrs, int defStyle) {
  26. super(context, attrs, defStyle);
  27. init(context, attrs);
  28. }
  29.  
  30. private void init(final @NonNull Context context, final @Nullable AttributeSet attrs) {
  31. final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.DashboardWithSquaredCells);
  32. try {
  33. mRows = Math.max(styledAttributes.getInt(R.styleable.DashboardWithSquaredCells_rows, 1), 1);
  34. mColumns = Math.max(styledAttributes.getInt(R.styleable.DashboardWithSquaredCells_columns, 1), 1);
  35. mDividerOffset = Math.max(styledAttributes.getDimensionPixelSize(R.styleable.DashboardWithSquaredCells_divider_offset, 0), 0);
  36. } finally {
  37. styledAttributes.recycle();
  38. }
  39. }
  40.  
  41. @Override
  42. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  43. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  44.  
  45. final int childSize = Math.max(0, (getMeasuredWidth() - mDividerOffset * (mColumns + 1)) / mColumns);
  46. final int childSizeSpec = MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.AT_MOST);
  47.  
  48. int visibleChilds = 0;
  49. for (int childIndex = 0, count = getChildCount(); childIndex < count; ++ childIndex) {
  50. final View child = getChildAt(childIndex);
  51. if (View.GONE != child.getVisibility()) {
  52. child.measure(childSizeSpec, childSizeSpec);
  53. ++ visibleChilds;
  54. }
  55. }
  56.  
  57. final int visibleRows = (visibleChilds / mColumns) + ((visibleChilds % mColumns) > 0 ? 1 : 0);
  58. final int manuallyMeasuredHeight = childSize * visibleRows + (visibleRows + 1) * mDividerOffset;
  59.  
  60. setMeasuredDimension(getMeasuredWidth(), manuallyMeasuredHeight);
  61. }
  62.  
  63. @SuppressWarnings("ResourceType")
  64. @Override
  65. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  66. final int availableWidth = r - l;
  67. final int availableHeight = b - t;
  68.  
  69. final int cellWidth = Math.max(0, (availableWidth - mDividerOffset * (mColumns + 1)) / mColumns);
  70. final int cellHeight = Math.max(0, (availableHeight - mDividerOffset * (mRows + 1)) / mRows);
  71.  
  72. int visibleChildIndex = 0;
  73. for (int childIndex = 0, count = getChildCount(); childIndex < count; childIndex++) {
  74. final View child = getChildAt(childIndex);
  75. if (View.GONE == child.getVisibility()) {
  76. continue;
  77. }
  78.  
  79. final int rowIndex = visibleChildIndex / mColumns;
  80. final int columnIndex = visibleChildIndex % mColumns;
  81.  
  82. final int currentLeft = mDividerOffset * (columnIndex + 1) + cellWidth * columnIndex;
  83. final int currentTop = mDividerOffset * (rowIndex + 1) + cellHeight * rowIndex;
  84. final int currentRight = (0 == mDividerOffset && columnIndex == mColumns - 1) ? r : (currentLeft + cellWidth);
  85. final int currentBottom = (0 == mDividerOffset && rowIndex == mRows - 1) ? b : (currentTop + cellHeight);
  86.  
  87. child.layout(currentLeft, currentTop, currentRight, currentBottom);
  88.  
  89. ++ visibleChildIndex;
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement