-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridViewAdapter.java
More file actions
148 lines (133 loc) · 4.7 KB
/
Copy pathGridViewAdapter.java
File metadata and controls
148 lines (133 loc) · 4.7 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.example.itay.sumika;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
public class GridViewAdapter extends BaseAdapter {
boolean gotZero=false;
Point pForGetView;
final String colorP1 = "blue";
String colorP2 = "pink";
Cell[][] cells;
int[][] cellsInInt;
int numOfRowsY, numOfColumns;
int validSpace, timeForMove;
Context context;
GameManager gm;
Point pForgetView;
private int btnSize, btnFontSize;
GridViewAdapter(int numOfColumnsX,
int numOfRowsY,
Context context,
int btnSize,
int btnFontSize,
GameScreen gameScreen,
boolean onePlayer,
int compLevel,
int validSpace,
int timeForMove) {
this.cells = new Cell[numOfColumnsX][numOfRowsY];
this.context = context;
this.numOfColumns = numOfColumnsX;
this.numOfRowsY = numOfRowsY;
this.btnFontSize = btnFontSize;
this.btnSize = btnSize;
this.validSpace=validSpace;
this.timeForMove=timeForMove;
this.cellsInInt=gameScreen.board.cellsToInt();
for (int y = 0; y < numOfRowsY; y++) {
for (int x = 0; x < numOfColumnsX; x++) {
Cell c = new Cell(x, y);
cells[x][y] = c;
}
}
//TODO one player is always false here
gm = new GameManager(colorP1, colorP2, cellsInInt, gameScreen,onePlayer,compLevel,validSpace,timeForMove);
}
public Cell[][] getCells() {
return cells;
}
@Override
public int getCount() {
int length = numOfColumns * numOfRowsY;
return length;
}
@Override
public Object getItem(int position) {
int x, y;
x = (position + 1) % numOfColumns;
y = (position + 1) / numOfColumns;
return cells[x][y];
}
@Override
public long getItemId(int position) {
return position;
}
//this getView get called 4 times for position 0, what i suspect that mess up my buttons for the comp
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
View row = convertView;
ViewHolder holder;
//checks if the gridview is new- to inflate only once.
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_item, viewGroup, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
//else- using the old holder.
else {
//using the Tag to save the findViewById
holder = (ViewHolder) row.getTag();
}
holder.btnSqure = changeBtnSize(holder.btnSqure);
final Button btn = holder.btnSqure;//final to call in inner class.
pForgetView = intPositionToPoint(position);
if(!gotZero||position!=0) {
gm.gameScreen.board.addButtonToCell(btn, pForgetView.x, pForgetView.y);
holder.btnSqure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (gm.getUserClicks) {
pForGetView = intPositionToPoint(position);
gm.game(pForGetView);
}
}
});
gotZero=true;
}
return row;
}
private Point intPositionToPoint(int position) {
Point p = new Point();
p.x = (position) % numOfColumns;
p.y = (position) / numOfColumns;
return p;
}
public Button changeBtnSize(Button btn) {
ViewGroup.LayoutParams params = btn.getLayoutParams();
//Button new width in px
params.width = btnSize;
params.height = btnSize;
btn.setLayoutParams(params);
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnFontSize);
return btn;
}
//using class holder for improving preformens. findViewById is very expensive.
class ViewHolder {
Button btnSqure;
ViewHolder(View v) {
btnSqure = v.findViewById(R.id.singleItemButton);
}
}
public void refresh(Cell[][] cells,Point position)
{
this.cells[position.x][position.y]=cells[position.x][position.y];
notifyDataSetChanged();
}
}