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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
/**
* <p>A layout that arranges its children horizontally. A TableRow should
* always be used as a child of a {@link android.widget.TableLayout}. If a
* TableRow's parent is not a TableLayout, the TableRow will behave as
* an horizontal {@link android.widget.LinearLayout}.</p>
*
* <p>The children of a TableRow do not need to specify the
* <code>layout_width</code> and <code>layout_height</code> attributes in the
* XML file. TableRow always enforces those values to be respectively
* {@link android.widget.TableLayout.LayoutParams#MATCH_PARENT} and
* {@link android.widget.TableLayout.LayoutParams#WRAP_CONTENT}.</p>
*
* <p>
* Also see {@link TableRow.LayoutParams android.widget.TableRow.LayoutParams}
* for layout attributes </p>
*/
public class TableRow extends LinearLayout {
private int mNumColumns = 0;
private int[] mColumnWidths;
private int[] mConstrainedColumnWidths;
private SparseIntArray mColumnToChildIndex;
private ChildrenTracker mChildrenTracker;
/**
* <p>Creates a new TableRow for the given context.</p>
*
* @param context the application environment
*/
public TableRow(Context context) {
super(context);
initTableRow();
}
/**
* <p>Creates a new TableRow for the given context and with the
* specified set attributes.</p>
*
* @param context the application environment
* @param attrs a collection of attributes
*/
public TableRow(Context context, AttributeSet attrs) {
super(context, attrs);
initTableRow();
}
private void initTableRow() {
OnHierarchyChangeListener oldListener = mOnHierarchyChangeListener;
mChildrenTracker = new ChildrenTracker();
if (oldListener != null) {
mChildrenTracker.setOnHierarchyChangeListener(oldListener);
}
super.setOnHierarchyChangeListener(mChildrenTracker);
}
/**
* {@inheritDoc}
*/
@Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
mChildrenTracker.setOnHierarchyChangeListener(listener);
}
/**
* <p>Collapses or restores a given column.</p>
*
* @param columnIndex the index of the column
* @param collapsed true if the column must be collapsed, false otherwise
* {@hide}
*/
void setColumnCollapsed(int columnIndex, boolean collapsed) {
View child = getVirtualChildAt(columnIndex);
if (child != null) {
child.setVisibility(collapsed ? GONE : VISIBLE);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// enforce horizontal layout
measureHorizontal(widthMeasureSpec, heightMeasureSpec);
}
/**
* {@inheritDoc}
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// enforce horizontal layout
layoutHorizontal();
}
/**
* {@inheritDoc}
*/
@Override
public View getVirtualChildAt(int i) {
if (mColumnToChildIndex == null) {
mapIndexAndColumns();
}
final int deflectedIndex = mColumnToChildIndex.get(i, -1);
if (deflectedIndex != -1) {
return getChildAt(deflectedIndex);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public int getVirtualChildCount() {
if (mColumnToChildIndex == null) {
mapIndexAndColumns();
}
return mNumColumns;
}
private void mapIndexAndColumns() {
if (mColumnToChildIndex == null) {
int virtualCount = 0;
final int count = getChildCount();
mColumnToChildIndex = new SparseIntArray();
final SparseIntArray columnToChild = mColumnToChildIndex;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.column >= virtualCount) {
virtualCount = layoutParams.column;
}
for (int j = 0; j < layoutParams.span; j++) {
columnToChild.put(virtualCount++, i);
}
}
mNumColumns = virtualCount;
}
}
/**
* {@inheritDoc}
*/
@Override
int measureNullChild(int childIndex) {
return mConstrainedColumnWidths[childIndex];
}
/**
* {@inheritDoc}
*/
@Override
void measureChildBeforeLayout(View child, int childIndex,
int widthMeasureSpec, int totalWidth,
int heightMeasureSpec, int totalHeight) {
if (mConstrainedColumnWidths != null) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
int measureMode = MeasureSpec.EXACTLY;
int columnWidth = 0;
final int span = lp.span;
final int[] constrainedColumnWidths = mConstrainedColumnWidths;
for (int i = 0; i < span; i++) {
columnWidth += constrainedColumnWidths[childIndex + i];
}
final int gravity = lp.gravity;
final boolean isHorizontalGravity = Gravity.isHorizontal(gravity);
if (isHorizontalGravity) {
measureMode = MeasureSpec.AT_MOST;
}
// no need to care about padding here,
// ViewGroup.getChildMeasureSpec() would get rid of it anyway
// because of the EXACTLY measure spec we use
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
Math.max(0, columnWidth - lp.leftMargin - lp.rightMargin), measureMode
);
int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin +
lp .bottomMargin + totalHeight, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
if (isHorizontalGravity) {
final int childWidth = child.getMeasuredWidth();
lp.mOffset[LayoutParams.LOCATION_NEXT] = columnWidth - childWidth;
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
// don't offset on X axis
break;
case Gravity.RIGHT:
lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT];
break;
case Gravity.CENTER_HORIZONTAL:
lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT] / 2;
break;
}
} else {
lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT] = 0;
}
} else {
// fail silently when column widths are not available
super.measureChildBeforeLayout(child, childIndex, widthMeasureSpec,
totalWidth, heightMeasureSpec, totalHeight);
}
}
/**
* {@inheritDoc}
*/
@Override
int getChildrenSkipCount(View child, int index) {
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
// when the span is 1 (default), we need to skip 0 child
return layoutParams.span - 1;
}
/**
* {@inheritDoc}
*/
@Override
int getLocationOffset(View child) {
return ((TableRow.LayoutParams) child.getLayoutParams()).mOffset[LayoutParams.LOCATION];
}
/**
* {@inheritDoc}
*/
@Override
int getNextLocationOffset(View child) {
return ((TableRow.LayoutParams) child.getLayoutParams()).mOffset[LayoutParams.LOCATION_NEXT];
}
/**
* <p>Measures the preferred width of each child, including its margins.</p>
*
* @param widthMeasureSpec the width constraint imposed by our parent
*
* @return an array of integers corresponding to the width of each cell, or
* column, in this row
* {@hide}
*/
int[] getColumnsWidths(int widthMeasureSpec) {
final int numColumns = getVirtualChildCount();
if (mColumnWidths == null || numColumns != mColumnWidths.length) {
mColumnWidths = new int[numColumns];
}
final int[] columnWidths = mColumnWidths;
for (int i = 0; i < numColumns; i++) {
final View child = getVirtualChildAt(i);
if (child != null && child.getVisibility() != GONE) {
final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.span == 1) {
int spec;
switch (layoutParams.width) {
case LayoutParams.WRAP_CONTENT:
spec = getChildMeasureSpec(widthMeasureSpec, 0, LayoutParams.WRAP_CONTENT);
break;
case LayoutParams.MATCH_PARENT:
spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
break;
default:
spec = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY);
}
child.measure(spec, spec);
final int width = child.getMeasuredWidth() + layoutParams.leftMargin +
layoutParams.rightMargin;
columnWidths[i] = width;
} else {
columnWidths[i] = 0;
}
} else {
columnWidths[i] = 0;
}
}
return columnWidths;
}
/**
* <p>Sets the width of all of the columns in this row. At layout time,
* this row sets a fixed width, as defined by <code>columnWidths</code>,
* on each child (or cell, or column.)</p>
*
* @param columnWidths the fixed width of each column that this row must
* honor
* @throws IllegalArgumentException when columnWidths' length is smaller
* than the number of children in this row
* {@hide}
*/
void setColumnsWidthConstraints(int[] columnWidths) {
if (columnWidths == null || columnWidths.length < getVirtualChildCount()) {
throw new IllegalArgumentException(
"columnWidths should be >= getVirtualChildCount()");
}
mConstrainedColumnWidths = columnWidths;
}
/**
* {@inheritDoc}
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new TableRow.LayoutParams(getContext(), attrs);
}
/**
* Returns a set of layout parameters with a width of
* {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT},
* a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning.
*/
@Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof TableRow.LayoutParams;
}
/**
* {@inheritDoc}
*/
@Override
protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(TableRow.class.getName());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(TableRow.class.getName());
}
/**
* <p>Set of layout parameters used in table rows.</p>
*
* @see android.widget.TableLayout.LayoutParams
*
* @attr ref android.R.styleable#TableRow_Cell_layout_column
* @attr ref android.R.styleable#TableRow_Cell_layout_span
*/
public static class LayoutParams extends LinearLayout.LayoutParams {
/**
* <p>The column index of the cell represented by the widget.</p>
*/
@ViewDebug.ExportedProperty(category = "layout")
public int column;
/**
* <p>The number of columns the widgets spans over.</p>
*/
@ViewDebug.ExportedProperty(category = "layout")
public int span;
private static final int LOCATION = 0;
private static final int LOCATION_NEXT = 1;
private int[] mOffset = new int[2];
/**
* {@inheritDoc}
*/
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a =
c.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.TableRow_Cell);
column = a.getInt(com.android.internal.R.styleable.TableRow_Cell_layout_column, -1);
span = a.getInt(com.android.internal.R.styleable.TableRow_Cell_layout_span, 1);
if (span <= 1) {
span = 1;
}
a.recycle();
}
/**
* <p>Sets the child width and the child height.</p>
*
* @param w the desired width
* @param h the desired height
*/
public LayoutParams(int w, int h) {
super(w, h);
column = -1;
span = 1;
}
/**
* <p>Sets the child width, height and weight.</p>
*
* @param w the desired width
* @param h the desired height
* @param initWeight the desired weight
*/
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
column = -1;
span = 1;
}
/**
* <p>Sets the child width to {@link android.view.ViewGroup.LayoutParams}
* and the child height to
* {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}.</p>
*/
public LayoutParams() {
super(MATCH_PARENT, WRAP_CONTENT);
column = -1;
span = 1;
}
/**
* <p>Puts the view in the specified column.</p>
*
* <p>Sets the child width to {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT}
* and the child height to
* {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}.</p>
*
* @param column the column index for the view
*/
public LayoutParams(int column) {
this();
this.column = column;
}
/**
* {@inheritDoc}
*/
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
/**
* {@inheritDoc}
*/
public LayoutParams(MarginLayoutParams source) {
super(source);
}
@Override
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
width = a.getLayoutDimension(widthAttr, MATCH_PARENT);
height = a.getLayoutDimension(heightAttr, WRAP_CONTENT);
}
}
// special transparent hierarchy change listener
private class ChildrenTracker implements OnHierarchyChangeListener {
private OnHierarchyChangeListener listener;
private void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
this.listener = listener;
}
public void onChildViewAdded(View parent, View child) {
// dirties the index to column map
mColumnToChildIndex = null;
if (this.listener != null) {
this.listener.onChildViewAdded(parent, child);
}
}
public void onChildViewRemoved(View parent, View child) {
// dirties the index to column map
mColumnToChildIndex = null;
if (this.listener != null) {
this.listener.onChildViewRemoved(parent, child);
}
}
}
}
|