xsnippet logotype

xsnippet

#363717, Untitled [ Java ]

by Guest
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
public class BoardView
        extends SquareLayout {

    private boolean isGenerated;
    private BoardListener listener;
    private Cell[][] values;

    public BoardView(Context context) {
        super(context);
    }

    public BoardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setListener(BoardListener listener) {
        this.listener = listener;
    }

    public void setValues(Cell[][] cells) {
        this.values = cells;
        regenerate();
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (isGenerated) {
            return;
        }

        regenerate();
    }

    private void regenerate() {
        removeAllViews();

        if (values == null || values.length == 0) {
            return;
        }

        isGenerated = true;
        setRowCount(values.length);
        setColumnCount(values[0].length);
        setAlignmentMode(ALIGN_BOUNDS);

        for (int x = 0; x < values.length; x++) {
            for (int y = 0; y < values[x].length; y++) {
                AutoResizeTextView cell = new AutoResizeTextView(getContext());
                cell.setTextSize(500);
                cell.setTextColor(Color.BLACK);
                cell.setGravity(Gravity.CENTER);
                cell.setOnClickListener(new OnItemCLickListener(x, y));
                cell.setSelected(values[x][y].isSelected());
                cell.setText(String.valueOf(values[x][y].getValue()));
                cell.setBackground(getResources().getDrawable(values[x][y].getColor()));

                addView(cell, createParamsForCell());
            }
        }
    }

    private LayoutParams createParamsForCell() {
        LayoutParams cellParams = new LayoutParams(spec(UNDEFINED, 1f), spec(UNDEFINED, 1f));
        cellParams.setMargins(5, 5, 5, 5);
        return cellParams;
    }


    public interface BoardListener {
        void onItemClicked(int x, int y, View view);
    }

    private class OnItemCLickListener
            implements View.OnClickListener {
        private int x, y;

        OnItemCLickListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public void onClick(View view) {
            if (listener != null) {
                listener.onItemClicked(x, y, view);
            }
        }
    }

    public interface Cell {
        boolean isSelected();
        int getValue();
        @DrawableRes int getColor();
    }
}