forked from zsol/android_frameworks_base
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGridView.java
More file actions
2120 lines (1817 loc) · 76 KB
/
Copy pathGridView.java
File metadata and controls
2120 lines (1817 loc) · 76 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.Intent;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;
import android.widget.RemoteViews.RemoteView;
/**
* A view that shows items in two-dimensional scrolling grid. The items in the
* grid come from the {@link ListAdapter} associated with this view.
*
* <p>See the <a href="{@docRoot}resources/tutorials/views/hello-gridview.html">Grid
* View tutorial</a>.</p>
*
* @attr ref android.R.styleable#GridView_horizontalSpacing
* @attr ref android.R.styleable#GridView_verticalSpacing
* @attr ref android.R.styleable#GridView_stretchMode
* @attr ref android.R.styleable#GridView_columnWidth
* @attr ref android.R.styleable#GridView_numColumns
* @attr ref android.R.styleable#GridView_gravity
*/
@RemoteView
public class GridView extends AbsListView {
/**
* Disables stretching.
*
* @see #setStretchMode(int)
*/
public static final int NO_STRETCH = 0;
/**
* Stretches the spacing between columns.
*
* @see #setStretchMode(int)
*/
public static final int STRETCH_SPACING = 1;
/**
* Stretches columns.
*
* @see #setStretchMode(int)
*/
public static final int STRETCH_COLUMN_WIDTH = 2;
/**
* Stretches the spacing between columns. The spacing is uniform.
*
* @see #setStretchMode(int)
*/
public static final int STRETCH_SPACING_UNIFORM = 3;
/**
* Creates as many columns as can fit on screen.
*
* @see #setNumColumns(int)
*/
public static final int AUTO_FIT = -1;
private int mNumColumns = AUTO_FIT;
private int mHorizontalSpacing = 0;
private int mRequestedHorizontalSpacing;
private int mVerticalSpacing = 0;
private int mStretchMode = STRETCH_COLUMN_WIDTH;
private int mColumnWidth;
private int mRequestedColumnWidth;
private int mRequestedNumColumns;
private View mReferenceView = null;
private View mReferenceViewInSelectedRow = null;
private int mGravity = Gravity.LEFT;
private final Rect mTempRect = new Rect();
public GridView(Context context) {
super(context);
}
public GridView(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.gridViewStyle);
}
public GridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.GridView, defStyle, 0);
int hSpacing = a.getDimensionPixelOffset(
com.android.internal.R.styleable.GridView_horizontalSpacing, 0);
setHorizontalSpacing(hSpacing);
int vSpacing = a.getDimensionPixelOffset(
com.android.internal.R.styleable.GridView_verticalSpacing, 0);
setVerticalSpacing(vSpacing);
int index = a.getInt(com.android.internal.R.styleable.GridView_stretchMode, STRETCH_COLUMN_WIDTH);
if (index >= 0) {
setStretchMode(index);
}
int columnWidth = a.getDimensionPixelOffset(com.android.internal.R.styleable.GridView_columnWidth, -1);
if (columnWidth > 0) {
setColumnWidth(columnWidth);
}
int numColumns = a.getInt(com.android.internal.R.styleable.GridView_numColumns, 1);
setNumColumns(numColumns);
index = a.getInt(com.android.internal.R.styleable.GridView_gravity, -1);
if (index >= 0) {
setGravity(index);
}
a.recycle();
}
@Override
public ListAdapter getAdapter() {
return mAdapter;
}
/**
* Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService
* through the specified intent.
* @param intent the intent used to identify the RemoteViewsService for the adapter to connect to.
*/
@android.view.RemotableViewMethod
public void setRemoteViewsAdapter(Intent intent) {
super.setRemoteViewsAdapter(intent);
}
/**
* Sets the data behind this GridView.
*
* @param adapter the adapter providing the grid's data
*/
@Override
public void setAdapter(ListAdapter adapter) {
if (mAdapter != null && mDataSetObserver != null) {
mAdapter.unregisterDataSetObserver(mDataSetObserver);
}
resetList();
mRecycler.clear();
mAdapter = adapter;
mOldSelectedPosition = INVALID_POSITION;
mOldSelectedRowId = INVALID_ROW_ID;
// AbsListView#setAdapter will update choice mode states.
super.setAdapter(adapter);
if (mAdapter != null) {
mOldItemCount = mItemCount;
mItemCount = mAdapter.getCount();
mDataChanged = true;
checkFocus();
mDataSetObserver = new AdapterDataSetObserver();
mAdapter.registerDataSetObserver(mDataSetObserver);
mRecycler.setViewTypeCount(mAdapter.getViewTypeCount());
int position;
if (mStackFromBottom) {
position = lookForSelectablePosition(mItemCount - 1, false);
} else {
position = lookForSelectablePosition(0, true);
}
setSelectedPositionInt(position);
setNextSelectedPositionInt(position);
checkSelectionChanged();
} else {
checkFocus();
// Nothing selected
checkSelectionChanged();
}
requestLayout();
}
@Override
int lookForSelectablePosition(int position, boolean lookDown) {
final ListAdapter adapter = mAdapter;
if (adapter == null || isInTouchMode()) {
return INVALID_POSITION;
}
if (position < 0 || position >= mItemCount) {
return INVALID_POSITION;
}
return position;
}
/**
* {@inheritDoc}
*/
@Override
void fillGap(boolean down) {
final int numColumns = mNumColumns;
final int verticalSpacing = mVerticalSpacing;
final int count = getChildCount();
if (down) {
int paddingTop = 0;
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
paddingTop = getListPaddingTop();
}
final int startOffset = count > 0 ?
getChildAt(count - 1).getBottom() + verticalSpacing : paddingTop;
int position = mFirstPosition + count;
if (mStackFromBottom) {
position += numColumns - 1;
}
fillDown(position, startOffset);
correctTooHigh(numColumns, verticalSpacing, getChildCount());
} else {
int paddingBottom = 0;
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
paddingBottom = getListPaddingBottom();
}
final int startOffset = count > 0 ?
getChildAt(0).getTop() - verticalSpacing : getHeight() - paddingBottom;
int position = mFirstPosition;
if (!mStackFromBottom) {
position -= numColumns;
} else {
position--;
}
fillUp(position, startOffset);
correctTooLow(numColumns, verticalSpacing, getChildCount());
}
}
/**
* Fills the list from pos down to the end of the list view.
*
* @param pos The first position to put in the list
*
* @param nextTop The location where the top of the item associated with pos
* should be drawn
*
* @return The view that is currently selected, if it happens to be in the
* range that we draw.
*/
private View fillDown(int pos, int nextTop) {
View selectedView = null;
int end = (mBottom - mTop);
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
end -= mListPadding.bottom;
}
while (nextTop < end && pos < mItemCount) {
View temp = makeRow(pos, nextTop, true);
if (temp != null) {
selectedView = temp;
}
// mReferenceView will change with each call to makeRow()
// do not cache in a local variable outside of this loop
nextTop = mReferenceView.getBottom() + mVerticalSpacing;
pos += mNumColumns;
}
return selectedView;
}
private View makeRow(int startPos, int y, boolean flow) {
final int columnWidth = mColumnWidth;
final int horizontalSpacing = mHorizontalSpacing;
int last;
int nextLeft = mListPadding.left +
((mStretchMode == STRETCH_SPACING_UNIFORM) ? horizontalSpacing : 0);
if (!mStackFromBottom) {
last = Math.min(startPos + mNumColumns, mItemCount);
} else {
last = startPos + 1;
startPos = Math.max(0, startPos - mNumColumns + 1);
if (last - startPos < mNumColumns) {
nextLeft += (mNumColumns - (last - startPos)) * (columnWidth + horizontalSpacing);
}
}
View selectedView = null;
final boolean hasFocus = shouldShowSelector();
final boolean inClick = touchModeDrawsInPressedState();
final int selectedPosition = mSelectedPosition;
View child = null;
for (int pos = startPos; pos < last; pos++) {
// is this the selected item?
boolean selected = pos == selectedPosition;
// does the list view have focus or contain focus
final int where = flow ? -1 : pos - startPos;
child = makeAndAddView(pos, y, flow, nextLeft, selected, where);
nextLeft += columnWidth;
if (pos < last - 1) {
nextLeft += horizontalSpacing;
}
if (selected && (hasFocus || inClick)) {
selectedView = child;
}
}
mReferenceView = child;
if (selectedView != null) {
mReferenceViewInSelectedRow = mReferenceView;
}
return selectedView;
}
/**
* Fills the list from pos up to the top of the list view.
*
* @param pos The first position to put in the list
*
* @param nextBottom The location where the bottom of the item associated
* with pos should be drawn
*
* @return The view that is currently selected
*/
private View fillUp(int pos, int nextBottom) {
View selectedView = null;
int end = 0;
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
end = mListPadding.top;
}
while (nextBottom > end && pos >= 0) {
View temp = makeRow(pos, nextBottom, false);
if (temp != null) {
selectedView = temp;
}
nextBottom = mReferenceView.getTop() - mVerticalSpacing;
mFirstPosition = pos;
pos -= mNumColumns;
}
if (mStackFromBottom) {
mFirstPosition = Math.max(0, pos + 1);
}
return selectedView;
}
/**
* Fills the list from top to bottom, starting with mFirstPosition
*
* @param nextTop The location where the top of the first item should be
* drawn
*
* @return The view that is currently selected
*/
private View fillFromTop(int nextTop) {
mFirstPosition = Math.min(mFirstPosition, mSelectedPosition);
mFirstPosition = Math.min(mFirstPosition, mItemCount - 1);
if (mFirstPosition < 0) {
mFirstPosition = 0;
}
mFirstPosition -= mFirstPosition % mNumColumns;
return fillDown(mFirstPosition, nextTop);
}
private View fillFromBottom(int lastPosition, int nextBottom) {
lastPosition = Math.max(lastPosition, mSelectedPosition);
lastPosition = Math.min(lastPosition, mItemCount - 1);
final int invertedPosition = mItemCount - 1 - lastPosition;
lastPosition = mItemCount - 1 - (invertedPosition - (invertedPosition % mNumColumns));
return fillUp(lastPosition, nextBottom);
}
private View fillSelection(int childrenTop, int childrenBottom) {
final int selectedPosition = reconcileSelectedPosition();
final int numColumns = mNumColumns;
final int verticalSpacing = mVerticalSpacing;
int rowStart;
int rowEnd = -1;
if (!mStackFromBottom) {
rowStart = selectedPosition - (selectedPosition % numColumns);
} else {
final int invertedSelection = mItemCount - 1 - selectedPosition;
rowEnd = mItemCount - 1 - (invertedSelection - (invertedSelection % numColumns));
rowStart = Math.max(0, rowEnd - numColumns + 1);
}
final int fadingEdgeLength = getVerticalFadingEdgeLength();
final int topSelectionPixel = getTopSelectionPixel(childrenTop, fadingEdgeLength, rowStart);
final View sel = makeRow(mStackFromBottom ? rowEnd : rowStart, topSelectionPixel, true);
mFirstPosition = rowStart;
final View referenceView = mReferenceView;
if (!mStackFromBottom) {
fillDown(rowStart + numColumns, referenceView.getBottom() + verticalSpacing);
pinToBottom(childrenBottom);
fillUp(rowStart - numColumns, referenceView.getTop() - verticalSpacing);
adjustViewsUpOrDown();
} else {
final int bottomSelectionPixel = getBottomSelectionPixel(childrenBottom,
fadingEdgeLength, numColumns, rowStart);
final int offset = bottomSelectionPixel - referenceView.getBottom();
offsetChildrenTopAndBottom(offset);
fillUp(rowStart - 1, referenceView.getTop() - verticalSpacing);
pinToTop(childrenTop);
fillDown(rowEnd + numColumns, referenceView.getBottom() + verticalSpacing);
adjustViewsUpOrDown();
}
return sel;
}
private void pinToTop(int childrenTop) {
if (mFirstPosition == 0) {
final int top = getChildAt(0).getTop();
final int offset = childrenTop - top;
if (offset < 0) {
offsetChildrenTopAndBottom(offset);
}
}
}
private void pinToBottom(int childrenBottom) {
final int count = getChildCount();
if (mFirstPosition + count == mItemCount) {
final int bottom = getChildAt(count - 1).getBottom();
final int offset = childrenBottom - bottom;
if (offset > 0) {
offsetChildrenTopAndBottom(offset);
}
}
}
@Override
int findMotionRow(int y) {
final int childCount = getChildCount();
if (childCount > 0) {
final int numColumns = mNumColumns;
if (!mStackFromBottom) {
for (int i = 0; i < childCount; i += numColumns) {
if (y <= getChildAt(i).getBottom()) {
return mFirstPosition + i;
}
}
} else {
for (int i = childCount - 1; i >= 0; i -= numColumns) {
if (y >= getChildAt(i).getTop()) {
return mFirstPosition + i;
}
}
}
}
return INVALID_POSITION;
}
/**
* Layout during a scroll that results from tracking motion events. Places
* the mMotionPosition view at the offset specified by mMotionViewTop, and
* then build surrounding views from there.
*
* @param position the position at which to start filling
* @param top the top of the view at that position
* @return The selected view, or null if the selected view is outside the
* visible area.
*/
private View fillSpecific(int position, int top) {
final int numColumns = mNumColumns;
int motionRowStart;
int motionRowEnd = -1;
if (!mStackFromBottom) {
motionRowStart = position - (position % numColumns);
} else {
final int invertedSelection = mItemCount - 1 - position;
motionRowEnd = mItemCount - 1 - (invertedSelection - (invertedSelection % numColumns));
motionRowStart = Math.max(0, motionRowEnd - numColumns + 1);
}
final View temp = makeRow(mStackFromBottom ? motionRowEnd : motionRowStart, top, true);
// Possibly changed again in fillUp if we add rows above this one.
mFirstPosition = motionRowStart;
final View referenceView = mReferenceView;
// We didn't have anything to layout, bail out
if (referenceView == null) {
return null;
}
final int verticalSpacing = mVerticalSpacing;
View above;
View below;
if (!mStackFromBottom) {
above = fillUp(motionRowStart - numColumns, referenceView.getTop() - verticalSpacing);
adjustViewsUpOrDown();
below = fillDown(motionRowStart + numColumns, referenceView.getBottom() + verticalSpacing);
// Check if we have dragged the bottom of the grid too high
final int childCount = getChildCount();
if (childCount > 0) {
correctTooHigh(numColumns, verticalSpacing, childCount);
}
} else {
below = fillDown(motionRowEnd + numColumns, referenceView.getBottom() + verticalSpacing);
adjustViewsUpOrDown();
above = fillUp(motionRowStart - 1, referenceView.getTop() - verticalSpacing);
// Check if we have dragged the bottom of the grid too high
final int childCount = getChildCount();
if (childCount > 0) {
correctTooLow(numColumns, verticalSpacing, childCount);
}
}
if (temp != null) {
return temp;
} else if (above != null) {
return above;
} else {
return below;
}
}
private void correctTooHigh(int numColumns, int verticalSpacing, int childCount) {
// First see if the last item is visible
final int lastPosition = mFirstPosition + childCount - 1;
if (lastPosition == mItemCount - 1 && childCount > 0) {
// Get the last child ...
final View lastChild = getChildAt(childCount - 1);
// ... and its bottom edge
final int lastBottom = lastChild.getBottom();
// This is bottom of our drawable area
final int end = (mBottom - mTop) - mListPadding.bottom;
// This is how far the bottom edge of the last view is from the bottom of the
// drawable area
int bottomOffset = end - lastBottom;
final View firstChild = getChildAt(0);
final int firstTop = firstChild.getTop();
// Make sure we are 1) Too high, and 2) Either there are more rows above the
// first row or the first row is scrolled off the top of the drawable area
if (bottomOffset > 0 && (mFirstPosition > 0 || firstTop < mListPadding.top)) {
if (mFirstPosition == 0) {
// Don't pull the top too far down
bottomOffset = Math.min(bottomOffset, mListPadding.top - firstTop);
}
// Move everything down
offsetChildrenTopAndBottom(bottomOffset);
if (mFirstPosition > 0) {
// Fill the gap that was opened above mFirstPosition with more rows, if
// possible
fillUp(mFirstPosition - (mStackFromBottom ? 1 : numColumns),
firstChild.getTop() - verticalSpacing);
// Close up the remaining gap
adjustViewsUpOrDown();
}
}
}
}
private void correctTooLow(int numColumns, int verticalSpacing, int childCount) {
if (mFirstPosition == 0 && childCount > 0) {
// Get the first child ...
final View firstChild = getChildAt(0);
// ... and its top edge
final int firstTop = firstChild.getTop();
// This is top of our drawable area
final int start = mListPadding.top;
// This is bottom of our drawable area
final int end = (mBottom - mTop) - mListPadding.bottom;
// This is how far the top edge of the first view is from the top of the
// drawable area
int topOffset = firstTop - start;
final View lastChild = getChildAt(childCount - 1);
final int lastBottom = lastChild.getBottom();
final int lastPosition = mFirstPosition + childCount - 1;
// Make sure we are 1) Too low, and 2) Either there are more rows below the
// last row or the last row is scrolled off the bottom of the drawable area
if (topOffset > 0 && (lastPosition < mItemCount - 1 || lastBottom > end)) {
if (lastPosition == mItemCount - 1 ) {
// Don't pull the bottom too far up
topOffset = Math.min(topOffset, lastBottom - end);
}
// Move everything up
offsetChildrenTopAndBottom(-topOffset);
if (lastPosition < mItemCount - 1) {
// Fill the gap that was opened below the last position with more rows, if
// possible
fillDown(lastPosition + (!mStackFromBottom ? 1 : numColumns),
lastChild.getBottom() + verticalSpacing);
// Close up the remaining gap
adjustViewsUpOrDown();
}
}
}
}
/**
* Fills the grid based on positioning the new selection at a specific
* location. The selection may be moved so that it does not intersect the
* faded edges. The grid is then filled upwards and downwards from there.
*
* @param selectedTop Where the selected item should be
* @param childrenTop Where to start drawing children
* @param childrenBottom Last pixel where children can be drawn
* @return The view that currently has selection
*/
private View fillFromSelection(int selectedTop, int childrenTop, int childrenBottom) {
final int fadingEdgeLength = getVerticalFadingEdgeLength();
final int selectedPosition = mSelectedPosition;
final int numColumns = mNumColumns;
final int verticalSpacing = mVerticalSpacing;
int rowStart;
int rowEnd = -1;
if (!mStackFromBottom) {
rowStart = selectedPosition - (selectedPosition % numColumns);
} else {
int invertedSelection = mItemCount - 1 - selectedPosition;
rowEnd = mItemCount - 1 - (invertedSelection - (invertedSelection % numColumns));
rowStart = Math.max(0, rowEnd - numColumns + 1);
}
View sel;
View referenceView;
int topSelectionPixel = getTopSelectionPixel(childrenTop, fadingEdgeLength, rowStart);
int bottomSelectionPixel = getBottomSelectionPixel(childrenBottom, fadingEdgeLength,
numColumns, rowStart);
sel = makeRow(mStackFromBottom ? rowEnd : rowStart, selectedTop, true);
// Possibly changed again in fillUp if we add rows above this one.
mFirstPosition = rowStart;
referenceView = mReferenceView;
adjustForTopFadingEdge(referenceView, topSelectionPixel, bottomSelectionPixel);
adjustForBottomFadingEdge(referenceView, topSelectionPixel, bottomSelectionPixel);
if (!mStackFromBottom) {
fillUp(rowStart - numColumns, referenceView.getTop() - verticalSpacing);
adjustViewsUpOrDown();
fillDown(rowStart + numColumns, referenceView.getBottom() + verticalSpacing);
} else {
fillDown(rowEnd + numColumns, referenceView.getBottom() + verticalSpacing);
adjustViewsUpOrDown();
fillUp(rowStart - 1, referenceView.getTop() - verticalSpacing);
}
return sel;
}
/**
* Calculate the bottom-most pixel we can draw the selection into
*
* @param childrenBottom Bottom pixel were children can be drawn
* @param fadingEdgeLength Length of the fading edge in pixels, if present
* @param numColumns Number of columns in the grid
* @param rowStart The start of the row that will contain the selection
* @return The bottom-most pixel we can draw the selection into
*/
private int getBottomSelectionPixel(int childrenBottom, int fadingEdgeLength,
int numColumns, int rowStart) {
// Last pixel we can draw the selection into
int bottomSelectionPixel = childrenBottom;
if (rowStart + numColumns - 1 < mItemCount - 1) {
bottomSelectionPixel -= fadingEdgeLength;
}
return bottomSelectionPixel;
}
/**
* Calculate the top-most pixel we can draw the selection into
*
* @param childrenTop Top pixel were children can be drawn
* @param fadingEdgeLength Length of the fading edge in pixels, if present
* @param rowStart The start of the row that will contain the selection
* @return The top-most pixel we can draw the selection into
*/
private int getTopSelectionPixel(int childrenTop, int fadingEdgeLength, int rowStart) {
// first pixel we can draw the selection into
int topSelectionPixel = childrenTop;
if (rowStart > 0) {
topSelectionPixel += fadingEdgeLength;
}
return topSelectionPixel;
}
/**
* Move all views upwards so the selected row does not interesect the bottom
* fading edge (if necessary).
*
* @param childInSelectedRow A child in the row that contains the selection
* @param topSelectionPixel The topmost pixel we can draw the selection into
* @param bottomSelectionPixel The bottommost pixel we can draw the
* selection into
*/
private void adjustForBottomFadingEdge(View childInSelectedRow,
int topSelectionPixel, int bottomSelectionPixel) {
// Some of the newly selected item extends below the bottom of the
// list
if (childInSelectedRow.getBottom() > bottomSelectionPixel) {
// Find space available above the selection into which we can
// scroll upwards
int spaceAbove = childInSelectedRow.getTop() - topSelectionPixel;
// Find space required to bring the bottom of the selected item
// fully into view
int spaceBelow = childInSelectedRow.getBottom() - bottomSelectionPixel;
int offset = Math.min(spaceAbove, spaceBelow);
// Now offset the selected item to get it into view
offsetChildrenTopAndBottom(-offset);
}
}
/**
* Move all views upwards so the selected row does not interesect the top
* fading edge (if necessary).
*
* @param childInSelectedRow A child in the row that contains the selection
* @param topSelectionPixel The topmost pixel we can draw the selection into
* @param bottomSelectionPixel The bottommost pixel we can draw the
* selection into
*/
private void adjustForTopFadingEdge(View childInSelectedRow,
int topSelectionPixel, int bottomSelectionPixel) {
// Some of the newly selected item extends above the top of the list
if (childInSelectedRow.getTop() < topSelectionPixel) {
// Find space required to bring the top of the selected item
// fully into view
int spaceAbove = topSelectionPixel - childInSelectedRow.getTop();
// Find space available below the selection into which we can
// scroll downwards
int spaceBelow = bottomSelectionPixel - childInSelectedRow.getBottom();
int offset = Math.min(spaceAbove, spaceBelow);
// Now offset the selected item to get it into view
offsetChildrenTopAndBottom(offset);
}
}
/**
* Smoothly scroll to the specified adapter position. The view will
* scroll such that the indicated position is displayed.
* @param position Scroll to this adapter position.
*/
@android.view.RemotableViewMethod
public void smoothScrollToPosition(int position) {
super.smoothScrollToPosition(position);
}
/**
* Smoothly scroll to the specified adapter position offset. The view will
* scroll such that the indicated position is displayed.
* @param offset The amount to offset from the adapter position to scroll to.
*/
@android.view.RemotableViewMethod
public void smoothScrollByOffset(int offset) {
super.smoothScrollByOffset(offset);
}
/**
* Fills the grid based on positioning the new selection relative to the old
* selection. The new selection will be placed at, above, or below the
* location of the new selection depending on how the selection is moving.
* The selection will then be pinned to the visible part of the screen,
* excluding the edges that are faded. The grid is then filled upwards and
* downwards from there.
*
* @param delta Which way we are moving
* @param childrenTop Where to start drawing children
* @param childrenBottom Last pixel where children can be drawn
* @return The view that currently has selection
*/
private View moveSelection(int delta, int childrenTop, int childrenBottom) {
final int fadingEdgeLength = getVerticalFadingEdgeLength();
final int selectedPosition = mSelectedPosition;
final int numColumns = mNumColumns;
final int verticalSpacing = mVerticalSpacing;
int oldRowStart;
int rowStart;
int rowEnd = -1;
if (!mStackFromBottom) {
oldRowStart = (selectedPosition - delta) - ((selectedPosition - delta) % numColumns);
rowStart = selectedPosition - (selectedPosition % numColumns);
} else {
int invertedSelection = mItemCount - 1 - selectedPosition;
rowEnd = mItemCount - 1 - (invertedSelection - (invertedSelection % numColumns));
rowStart = Math.max(0, rowEnd - numColumns + 1);
invertedSelection = mItemCount - 1 - (selectedPosition - delta);
oldRowStart = mItemCount - 1 - (invertedSelection - (invertedSelection % numColumns));
oldRowStart = Math.max(0, oldRowStart - numColumns + 1);
}
final int rowDelta = rowStart - oldRowStart;
final int topSelectionPixel = getTopSelectionPixel(childrenTop, fadingEdgeLength, rowStart);
final int bottomSelectionPixel = getBottomSelectionPixel(childrenBottom, fadingEdgeLength,
numColumns, rowStart);
// Possibly changed again in fillUp if we add rows above this one.
mFirstPosition = rowStart;
View sel;
View referenceView;
if (rowDelta > 0) {
/*
* Case 1: Scrolling down.
*/
final int oldBottom = mReferenceViewInSelectedRow == null ? 0 :
mReferenceViewInSelectedRow.getBottom();
sel = makeRow(mStackFromBottom ? rowEnd : rowStart, oldBottom + verticalSpacing, true);
referenceView = mReferenceView;
adjustForBottomFadingEdge(referenceView, topSelectionPixel, bottomSelectionPixel);
} else if (rowDelta < 0) {
/*
* Case 2: Scrolling up.
*/
final int oldTop = mReferenceViewInSelectedRow == null ?
0 : mReferenceViewInSelectedRow .getTop();
sel = makeRow(mStackFromBottom ? rowEnd : rowStart, oldTop - verticalSpacing, false);
referenceView = mReferenceView;
adjustForTopFadingEdge(referenceView, topSelectionPixel, bottomSelectionPixel);
} else {
/*
* Keep selection where it was
*/
final int oldTop = mReferenceViewInSelectedRow == null ?
0 : mReferenceViewInSelectedRow .getTop();
sel = makeRow(mStackFromBottom ? rowEnd : rowStart, oldTop, true);
referenceView = mReferenceView;
}
if (!mStackFromBottom) {
fillUp(rowStart - numColumns, referenceView.getTop() - verticalSpacing);
adjustViewsUpOrDown();
fillDown(rowStart + numColumns, referenceView.getBottom() + verticalSpacing);
} else {
fillDown(rowEnd + numColumns, referenceView.getBottom() + verticalSpacing);
adjustViewsUpOrDown();
fillUp(rowStart - 1, referenceView.getTop() - verticalSpacing);
}
return sel;
}
private boolean determineColumns(int availableSpace) {
final int requestedHorizontalSpacing = mRequestedHorizontalSpacing;
final int stretchMode = mStretchMode;
final int requestedColumnWidth = mRequestedColumnWidth;
boolean didNotInitiallyFit = false;
if (mRequestedNumColumns == AUTO_FIT) {
if (requestedColumnWidth > 0) {
// Client told us to pick the number of columns
mNumColumns = (availableSpace + requestedHorizontalSpacing) /
(requestedColumnWidth + requestedHorizontalSpacing);
} else {
// Just make up a number if we don't have enough info
mNumColumns = 2;
}
} else {
// We picked the columns
mNumColumns = mRequestedNumColumns;
}
if (mNumColumns <= 0) {
mNumColumns = 1;
}
switch (stretchMode) {
case NO_STRETCH:
// Nobody stretches
mColumnWidth = requestedColumnWidth;
mHorizontalSpacing = requestedHorizontalSpacing;
break;
default:
int spaceLeftOver = availableSpace - (mNumColumns * requestedColumnWidth) -
((mNumColumns - 1) * requestedHorizontalSpacing);
if (spaceLeftOver < 0) {
didNotInitiallyFit = true;
}
switch (stretchMode) {
case STRETCH_COLUMN_WIDTH:
// Stretch the columns
mColumnWidth = requestedColumnWidth + spaceLeftOver / mNumColumns;
mHorizontalSpacing = requestedHorizontalSpacing;
break;
case STRETCH_SPACING:
// Stretch the spacing between columns
mColumnWidth = requestedColumnWidth;
if (mNumColumns > 1) {
mHorizontalSpacing = requestedHorizontalSpacing +
spaceLeftOver / (mNumColumns - 1);
} else {
mHorizontalSpacing = requestedHorizontalSpacing + spaceLeftOver;
}
break;
case STRETCH_SPACING_UNIFORM:
// Stretch the spacing between columns
mColumnWidth = requestedColumnWidth;
if (mNumColumns > 1) {
mHorizontalSpacing = requestedHorizontalSpacing +
spaceLeftOver / (mNumColumns + 1);
} else {
mHorizontalSpacing = requestedHorizontalSpacing + spaceLeftOver;
}
break;
}
break;
}
return didNotInitiallyFit;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding