-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathAdaptiveTableManager.java
374 lines (336 loc) · 8.85 KB
/
AdaptiveTableManager.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.cleveroad.adaptivetablelayout;
/**
* Manage row's heights and column's widths.
* Work flow:
* 1) Create object AdaptiveTableManager
* 2) Call {@link #init(int, int)}
* 3) Put data using {@link #putRowHeight(int, int)} and {@link #putColumnWidth(int, int)}
* 4) Call invalidate
* <p>
* In case changing full width or count of rows or columns, you need to re-init manager.(steps 2 - 4)
*/
class AdaptiveTableManager {
/**
* Contains full width (columns widths)
*/
private long mFullWidth;
/**
* Contains full height (rows heights)
*/
private long mFullHeight;
/**
* Array with column's widths
*/
private int[] mColumnWidths;
/**
* Array with row's heights
*/
private int[] mRowHeights;
/**
* Column's header height
*/
private int mHeaderColumnHeight;
/**
* Column's row width
*/
private int mHeaderRowWidth;
private boolean mIsInited = false;
void clear() {
// clear objects
mRowHeights = new int[0];
mColumnWidths = new int[0];
mFullWidth = 0;
mFullHeight = 0;
mHeaderColumnHeight = 0;
mHeaderRowWidth = 0;
mIsInited = false;
}
void init(int rowCount, int columnCount) {
if (rowCount < 0) {
rowCount = 0;
}
if (columnCount < 0) {
columnCount = 0;
}
// create objects
mRowHeights = new int[rowCount];
mColumnWidths = new int[columnCount];
mIsInited = true;
}
void checkForInit() {
if (!mIsInited) {
throw new IllegalStateException("You need to init matrix before work!");
}
}
void invalidate() {
checkForInit();
// calculate widths
mFullWidth = 0;
for (int itemWidth : mColumnWidths) {
mFullWidth += itemWidth;
}
// calculate heights
mFullHeight = 0;
for (int itemHeight : mRowHeights) {
mFullHeight += itemHeight;
}
}
/**
* @param column column index
* @return column's width
*/
int getColumnWidth(int column) {
checkForInit();
return mColumnWidths[column];
}
/**
* Put column width to the array. Call {@link #invalidate()} after all changes.
*
* @param column column index
* @param width column's width
*/
void putColumnWidth(int column, int width) {
checkForInit();
mColumnWidths[column] = width;
}
/**
* @param from from column index
* @param to to column index
* @return columns width
*/
int getColumnsWidth(int from, int to) {
checkForInit();
int width = 0;
for (int i = from; i < to && mColumnWidths != null; i++) {
width += mColumnWidths[i];
}
return width;
}
/**
* @return columns count
*/
int getColumnCount() {
checkForInit();
if (mColumnWidths != null) {
return mColumnWidths.length;
}
return 0;
}
/**
* @param row row index
* @return row's height
*/
int getRowHeight(int row) {
checkForInit();
return mRowHeights[row];
}
/**
* Put row height to the array. Call {@link #invalidate()} after all changes.
*
* @param row row index
* @param height row's height
*/
void putRowHeight(int row, int height) {
checkForInit();
mRowHeights[row] = height;
}
/**
* @param from from row index
* @param to to row index
* @return rows height
*/
int getRowsHeight(int from, int to) {
checkForInit();
int height = 0;
for (int i = from; i < to && mRowHeights != null; i++) {
height += mRowHeights[i];
}
return height;
}
/**
* @return rows count
*/
int getRowCount() {
checkForInit();
if (mRowHeights != null) {
return mRowHeights.length;
}
return 0;
}
/**
* @return columns width with row's header width
*/
long getFullWidth() {
checkForInit();
return mFullWidth + mHeaderRowWidth;
}
/**
* @return rows height with column's header height
*/
long getFullHeight() {
checkForInit();
return mFullHeight + mHeaderColumnHeight;
}
/**
* Return column number which bounds contains X
*
* @param x coordinate
* @return column number
*/
int getColumnByX(int x) {
checkForInit();
int sum = 0;
// header offset
int tempX = x - mHeaderRowWidth;
if (tempX <= sum) {
return 0;
}
for (int count = mColumnWidths.length, i = 0; i < count; i++) {
int nextSum = sum + mColumnWidths[i];
if (tempX > sum && tempX < nextSum) {
return i;
} else if (tempX < nextSum) {
return i - 1;
}
sum = nextSum;
}
return mColumnWidths.length - 1;
}
/**
* Return column number which bounds contains X
*
* @param x coordinate
* @return column number
*/
int getColumnByXWithShift(int x, int shiftEveryStep) {
checkForInit();
int sum = 0;
// header offset
int tempX = x - mHeaderRowWidth;
if (tempX <= sum) {
return 0;
}
for (int count = mColumnWidths.length, i = 0; i < count; i++) {
int nextSum = sum + mColumnWidths[i] + shiftEveryStep;
if (tempX > sum && tempX < nextSum) {
return i;
} else if (tempX < nextSum) {
return i - 1;
}
sum = nextSum;
}
return mColumnWidths.length - 1;
}
/**
* Return row number which bounds contains Y
*
* @param y coordinate
* @return row number
*/
int getRowByY(int y) {
checkForInit();
int sum = 0;
// header offset
int tempY = y - mHeaderColumnHeight;
if (tempY <= sum) {
return 0;
}
for (int count = mRowHeights.length, i = 0; i < count; i++) {
int nextSum = sum + mRowHeights[i];
if (tempY > sum && tempY < nextSum) {
return i;
} else if (tempY < nextSum) {
return i - 1;
}
sum = nextSum;
}
return mRowHeights.length - 1;
}
/**
* Return row number which bounds contains Y
*
* @param y coordinate
* @return row number
*/
int getRowByYWithShift(int y, int shiftEveryStep) {
checkForInit();
int sum = 0;
// header offset
int tempY = y - mHeaderColumnHeight;
if (tempY <= sum) {
return 0;
}
for (int count = mRowHeights.length, i = 0; i < count; i++) {
int nextSum = sum + mRowHeights[i] + shiftEveryStep;
if (tempY > sum && tempY < nextSum) {
return i;
} else if (tempY < nextSum) {
return i - 1;
}
sum = nextSum;
}
return mRowHeights.length - 1;
}
/**
* @return column's header height
*/
int getHeaderColumnHeight() {
checkForInit();
return mHeaderColumnHeight;
}
/**
* Set column's header height.
*
* @param headerColumnHeight column's header height.
*/
void setHeaderColumnHeight(int headerColumnHeight) {
checkForInit();
mHeaderColumnHeight = headerColumnHeight;
}
/**
* @return row's header width
*/
int getHeaderRowWidth() {
checkForInit();
return mHeaderRowWidth;
}
/**
* Set row's header width.
*
* @param headerRowWidth row's header width.
*/
void setHeaderRowWidth(int headerRowWidth) {
checkForInit();
mHeaderRowWidth = headerRowWidth;
}
/**
* Switch 2 items in array with columns data
*
* @param columnIndex from column index
* @param columnToIndex to column index
*/
void switchTwoColumns(int columnIndex, int columnToIndex) {
checkForInit();
int cellData = mColumnWidths[columnToIndex];
mColumnWidths[columnToIndex] = mColumnWidths[columnIndex];
mColumnWidths[columnIndex] = cellData;
}
/**
* Switch 2 items in array with rows data
*
* @param rowIndex from row index
* @param rowToIndex to row index
*/
void switchTwoRows(int rowIndex, int rowToIndex) {
checkForInit();
int cellData = mRowHeights[rowToIndex];
mRowHeights[rowToIndex] = mRowHeights[rowIndex];
mRowHeights[rowIndex] = cellData;
}
public int[] getColumnWidths() {
return mColumnWidths;
}
public int[] getRowHeights() {
return mRowHeights;
}
}