-
Notifications
You must be signed in to change notification settings - Fork 200
/
Gallery.java
1520 lines (1247 loc) · 40.6 KB
/
Gallery.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
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
package com.scvngr.levelup.views.gallery;
/*
* Based on the android GalleryView source. Modified to allow the view to snap to the
* left rather than the center. The biggest changes in this class are marked with comments
* with "Note:".
*
* 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.
*/
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.Transformation;
import android.widget.Scroller;
/**
* A view that shows items in a center-locked, horizontally scrolling list.
* <p>
* The default values for the Gallery assume you will be using
* {@link android.R.styleable#Theme_GalleryItemBackground} as the background for
* each View given to the Gallery from the Adapter. If you are not doing this,
* you may need to adjust some Gallery properties, such as the spacing.
* <p>
* Views given to the Gallery should use {@link Gallery.LayoutParams} as their
* layout parameters type.
*
* <p>
* See the <a href="{@docRoot}
* resources/tutorials/views/hello-Gallery.html">Gallery tutorial</a>.
* </p>
*
* CHECKSTYLE:OFF Android comments
*
* @attr ref android.R.styleable#Gallery_animationDuration
* @attr ref android.R.styleable#Gallery_spacing
* @attr ref android.R.styleable#Gallery_gravity CHECKSTYLE:ON
*/
public final class Gallery extends AbsSpinner implements GestureDetector.OnGestureListener {
/**
* Duration in milliseconds from the start of a scroll during which we're
* unsure whether the user is scrolling or flinging.
*/
private static final int SCROLL_TO_FLING_UNCERTAINTY_TIMEOUT = 250;
/**
* Horizontal spacing between items.
*/
private int mSpacing = 0;
/**
* How long the transition animation should run when a child view changes
* position, measured in milliseconds.
*/
// CHECKSTYLE:OFF not a magic number
private int mAnimationDuration = 400;
// CHECKSTYLE:ON
/**
* The alpha of items that are not selected.
*/
private float mUnselectedAlpha;
/**
* Left most edge of a child seen so far during layout.
*/
private int mLeftMost;
/**
* Right most edge of a child seen so far during layout.
*/
private int mRightMost;
private int mGravity;
/**
* Helper for detecting touch gestures.
*/
private final GestureDetector mGestureDetector;
/**
* The position of the item that received the user's down touch.
*/
private int mDownTouchPosition;
/**
* The view of the item that received the user's down touch.
*/
private View mDownTouchView;
/**
* Executes the delta scrolls from a fling or scroll movement.
*/
private final FlingRunnable mFlingRunnable = new FlingRunnable();
/**
* Sets mSuppressSelectionChanged = false. This is used to set it to false
* in the future. It will also trigger a selection changed.
*/
private final Runnable mDisableSuppressSelectionChangedRunnable = new Runnable() {
@Override
public void run() {
mSuppressSelectionChanged = false;
selectionChanged();
}
};
/**
* When fling runnable runs, it resets this to false. Any method along the
* path until the end of its run() can set this to true to abort any
* remaining fling. For example, if we've reached either the leftmost or
* rightmost item, we will set this to true.
*/
private boolean mShouldStopFling;
/**
* The currently selected item's child.
*/
private View mSelectedChild;
/**
* Whether to continuously callback on the item selected listener during a
* fling.
*/
private boolean mShouldCallbackDuringFling = true;
/**
* Whether to callback when an item that is not selected is clicked.
*/
private boolean mShouldCallbackOnUnselectedItemClick = true;
/**
* If true, do not callback to item selected listener.
*/
private boolean mSuppressSelectionChanged;
/**
* If true, we have received the "invoke" (center or enter buttons) key
* down. This is checked before we action on the "invoke" key up, and is
* subsequently cleared.
*/
private boolean mReceivedInvokeKeyDown;
private AdapterContextMenuInfo mContextMenuInfo;
/**
* If true, this onScroll is the first for this user's drag (remember, a
* drag sends many onScrolls).
*/
private boolean mIsFirstScroll;
/**
* If true, mFirstPosition is the position of the rightmost child, and the
* children are ordered right to left.
*/
private boolean mIsRtl = true;
private boolean mScrollToChildAfterItemClickEnabled;
private boolean mRightSpacingEnabled;
/**
* Constructor.
*
* @param context the context to inflate into
*/
public Gallery(final Context context) {
this(context, null);
}
/**
* Constructor for XML inflation.
*
* @param context the context to inflate with
* @param attrs the xml attrs
*/
public Gallery(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.galleryStyle);
}
/**
* Constructor for XML inflation.
*
* @param context the context to inflate with
* @param attrs the XML attrs
* @param defStyle the default style resource id
*/
public Gallery(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
mGestureDetector = new GestureDetector(context, this);
mGestureDetector.setIsLongpressEnabled(true);
setScrollAfterItemClickEnabled(true);
setScrollRightSpacingEnabled(true);
}
@Override
public boolean dispatchKeyEvent(final KeyEvent event) {
// Gallery steals all key events
return event.dispatch(this, null, null);
}
@Override
public void dispatchSetSelected(final boolean selected) {
/*
* We don't want to pass the selected state given from its parent to its
* children since this widget itself has a selected state to give to its
* children.
*/
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(final AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
public boolean onDown(final MotionEvent e) {
// Kill any existing fling/scroll
mFlingRunnable.stop(false);
// Get the item's view that was touched
mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY());
if (mDownTouchPosition >= 0) {
mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition);
mDownTouchView.setPressed(true);
}
// Reset the multiple-scroll tracking state
mIsFirstScroll = true;
// Must return true to get matching events for this down event.
return true;
}
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (!mShouldCallbackDuringFling) {
// We want to suppress selection changes
// Remove any future code to set mSuppressSelectionChanged = false
removeCallbacks(mDisableSuppressSelectionChangedRunnable);
// This will get reset once we scroll into slots
if (!mSuppressSelectionChanged) {
mSuppressSelectionChanged = true;
}
}
// Fling the Gallery!
mFlingRunnable.startUsingVelocity((int) -velocityX);
return true;
}
/**
* Handles left, right, and clicking.
*
* @see android.view.View#onKeyDown
*/
// CHECKSTYLE:OFF unmodified
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (movePrevious()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
}
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if (moveNext()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
}
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
mReceivedInvokeKeyDown = true;
// fallthrough to default handling
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER: {
if (mReceivedInvokeKeyDown) {
if (mItemCount > 0) {
dispatchPress(mSelectedChild);
postDelayed(new Runnable() {
@Override
public void run() {
dispatchUnpress();
}
}, ViewConfiguration.getPressedStateDuration());
final int selectedIndex = mSelectedPosition - mFirstPosition;
performItemClick(getChildAt(selectedIndex), mSelectedPosition,
mAdapter.getItemId(mSelectedPosition));
}
}
// Clear the flag
mReceivedInvokeKeyDown = false;
return true;
}
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onLongPress(final MotionEvent e) {
if (mDownTouchPosition < 0) return;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
final long id = getItemIdAtPosition(mDownTouchPosition);
dispatchLongPress(mDownTouchView, mDownTouchPosition, id);
}
@Override
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
/*
* Now's a good time to tell our parent to stop intercepting our events!
* The user has moved more than the slop amount, since GestureDetector
* ensures this before calling this method. Also, if a parent is more
* interested in this touch's events than we are, it would have
* intercepted them by now (for example, we can assume when a Gallery is
* in the ListView, a vertical scroll would not end up in this method
* since a ListView would have intercepted it by now).
*/
getParent().requestDisallowInterceptTouchEvent(true);
/*
* As the user scrolls, we want to callback selection changes so
* related- info on the screen is up-to-date with the Gallery's
* selection
*/
if (!mShouldCallbackDuringFling) {
if (mIsFirstScroll) {
/*
* We're not notifying the client of selection changes during
* the fling, and this scroll could possibly be a fling. Don't
* do selection changes until we're sure it is not a fling.
*/
if (!mSuppressSelectionChanged) {
mSuppressSelectionChanged = true;
}
postDelayed(mDisableSuppressSelectionChangedRunnable, SCROLL_TO_FLING_UNCERTAINTY_TIMEOUT);
}
} else {
if (mSuppressSelectionChanged) {
mSuppressSelectionChanged = false;
}
}
// Track the motion
trackMotionScroll(-1 * (int) distanceX);
mIsFirstScroll = false;
return true;
}
@Override
public void onShowPress(final MotionEvent e) {
}
@Override
public boolean onSingleTapUp(final MotionEvent e) {
if (mDownTouchPosition >= 0) {
if (mScrollToChildAfterItemClickEnabled) {
// An item tap should make it selected, so scroll to this child.
scrollToChild(mDownTouchPosition - mFirstPosition);
}
// Also pass the click so the client knows, if it wants to.
if (mShouldCallbackOnUnselectedItemClick || mDownTouchPosition == mSelectedPosition) {
performItemClick(mDownTouchView, mDownTouchPosition, mAdapter.getItemId(mDownTouchPosition));
}
return true;
}
return false;
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
if (!isEnabled()) return false;
// Give everything to the gesture detector
final boolean retValue = mGestureDetector.onTouchEvent(event);
final int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
// Helper method for lifted finger
onUp();
} else if (action == MotionEvent.ACTION_CANCEL) {
onCancel();
}
return retValue;
}
/**
* Sets how long the transition animation should run when a child view
* changes position. Only relevant if animation is turned on.
*
* @param animationDurationMillis The duration of the transition, in
* milliseconds.
*
* @attr ref android.R.styleable#Gallery_animationDuration
*/
public void setAnimationDuration(final int animationDurationMillis) {
mAnimationDuration = animationDurationMillis;
}
/**
* Whether or not to callback on any {@link #getOnItemSelectedListener()}
* while the items are being flinged. If false, only the final selected item
* will cause the callback. If true, all items between the first and the
* final will cause callbacks.
*
* @param shouldCallback Whether or not to callback on the listener while
* the items are being flinged.
*/
public void setCallbackDuringFling(final boolean shouldCallback) {
mShouldCallbackDuringFling = shouldCallback;
}
/**
* Whether or not to callback when an item that is not selected is clicked.
* If false, the item will become selected (and re-centered). If true, the
* {@link #getOnItemClickListener()} will get the callback.
*
* @param shouldCallback Whether or not to callback on the listener when a
* item that is not selected is clicked.
* @hide
*/
public void setCallbackOnUnselectedItemClick(final boolean shouldCallback) {
mShouldCallbackOnUnselectedItemClick = shouldCallback;
}
/**
* Describes how the child views are aligned.
*
* @param gravity the gravity to set
*
* @attr ref android.R.styleable#Gallery_gravity
*/
public void setGravity(final int gravity) {
if (mGravity != gravity) {
mGravity = gravity;
requestLayout();
}
}
public void setScrollAfterItemClickEnabled(final boolean enabled) {
mScrollToChildAfterItemClickEnabled = enabled;
}
public void setScrollRightSpacingEnabled(final boolean enabled) {
mRightSpacingEnabled = enabled;
}
/**
* Sets the spacing between items in a Gallery.
*
* @param spacing The spacing in pixels between items in the Gallery
*
* @attr ref android.R.styleable#Gallery_spacing
*/
public void setSpacing(final int spacing) {
mSpacing = spacing;
}
/**
* Sets the alpha of items that are not selected in the Gallery.
*
* @param unselectedAlpha the alpha for the items that are not selected.
*
* @attr ref android.R.styleable#Gallery_unselectedAlpha
*/
public void setUnselectedAlpha(final float unselectedAlpha) {
mUnselectedAlpha = unselectedAlpha;
}
@Override
public boolean showContextMenu() {
if (isPressed() && mSelectedPosition >= 0) {
final int index = mSelectedPosition - mFirstPosition;
final View v = getChildAt(index);
return dispatchLongPress(v, mSelectedPosition, mSelectedRowId);
}
return false;
}
@Override
public boolean showContextMenuForChild(final View originalView) {
final int longPressPosition = getPositionForView(originalView);
if (longPressPosition < 0) return false;
final long longPressId = mAdapter.getItemId(longPressPosition);
return dispatchLongPress(originalView, longPressPosition, longPressId);
}
@Override
protected boolean checkLayoutParams(final ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected int computeHorizontalScrollExtent() {
// Only 1 item is considered to be selected
return 1;
}
@Override
protected int computeHorizontalScrollOffset() {
// Current scroll position is the same as the selected position
return mSelectedPosition;
}
@Override
protected int computeHorizontalScrollRange() {
// Scroll range is the same as the item count
return mItemCount;
}
@Override
protected void dispatchSetPressed(final boolean pressed) {
// Show the pressed state on the selected child
if (mSelectedChild != null) {
mSelectedChild.setPressed(pressed);
}
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
/*
* Gallery expects Gallery.LayoutParams.
*/
return new Gallery.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(final ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
// CHECKSTYLE:ON
@Override
protected int getChildDrawingOrder(final int childCount, final int i) {
final int selectedIndex = mSelectedPosition - mFirstPosition;
// Just to be safe
if (selectedIndex < 0) return i;
if (i == childCount - 1)
// Draw the selected child last
return selectedIndex;
else if (i >= selectedIndex) // Move the children after the selected
// child earlier one
return i + 1;
else
// Keep the children before the selected child the same
return i;
}
// CHECKSTYLE:OFF unchanged method
@Override
protected boolean getChildStaticTransformation(final View child, final Transformation t) {
t.clear();
t.setAlpha(child == mSelectedChild ? 1.0f : mUnselectedAlpha);
return true;
}
@Override
protected ContextMenuInfo getContextMenuInfo() {
return mContextMenuInfo;
}
@Override
protected void onFocusChanged(final boolean gainFocus, final int direction, final Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
/*
* The Gallery shows focus by focusing the selected item. So, give focus
* to our selected item instead. We steal keys from our selected item
* elsewhere.
*/
if (gainFocus && mSelectedChild != null) {
mSelectedChild.requestFocus(direction);
mSelectedChild.setSelected(true);
}
}
@Override
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {
super.onLayout(changed, l, t, r, b);
/*
* Remember that we are in layout to prevent more layout request from
* being generated.
*/
mInLayout = true;
layout(0, false);
mInLayout = false;
}
/**
* Figure out vertical placement based on mGravity.
*
* @param child Child to place
* @param duringLayout if this is being called during layout
* @return Where the top of the child should be
*/
private int calculateTop(final View child, final boolean duringLayout) {
// CHECKSTYLE:OFF unmodified
final int myHeight = duringLayout ? getMeasuredHeight() : getHeight();
final int childHeight = duringLayout ? child.getMeasuredHeight() : child.getHeight();
int childTop = 0;
switch (mGravity) {
case Gravity.TOP:
childTop = mSpinnerPadding.top;
break;
case Gravity.CENTER_VERTICAL:
final int availableSpace = myHeight - mSpinnerPadding.bottom - mSpinnerPadding.top - childHeight;
childTop = mSpinnerPadding.top + availableSpace / 2;
break;
case Gravity.BOTTOM:
childTop = myHeight - mSpinnerPadding.bottom - childHeight;
break;
}
return childTop;
// CHECKSTYLE:ON
}
// CHECKSTYLE:ON
/**
* Detaches children that are off the screen (i.e.: Gallery bounds).
*
* @param toLeft Whether to detach children to the left of the Gallery, or
* to the right.
*/
private void detachOffScreenChildren(final boolean toLeft) {
final int numChildren = getChildCount();
final int firstPosition = mFirstPosition;
int start = 0;
int count = 0;
if (toLeft) {
final int galleryLeft = getPaddingLeft();
for (int i = 0; i < numChildren; i++) {
// CHECKSTYLE:OFF unmodified
final int n = mIsRtl ? numChildren - 1 - i : i;
// CHECKSTYLE:ON
final View child = getChildAt(n);
if (child.getRight() >= galleryLeft) {
break;
} else {
start = n;
count++;
mRecycler.put(firstPosition + n, child);
}
}
if (!mIsRtl) {
start = 0;
}
} else {
final int galleryRight = getWidth() - getPaddingRight();
for (int i = numChildren - 1; i >= 0; i--) {
// CHECKSTYLE:OFF unmodified
final int n = mIsRtl ? numChildren - 1 - i : i;
// CHECKSTYLE:ON
final View child = getChildAt(n);
if (child.getLeft() <= galleryRight) {
break;
} else {
start = n;
count++;
mRecycler.put(firstPosition + n, child);
}
}
if (mIsRtl) {
start = 0;
}
}
detachViewsFromParent(start, count);
if (toLeft != mIsRtl) {
mFirstPosition += count;
}
}
// CHECKSTYLE:OFF unmodified
private boolean dispatchLongPress(final View view, final int position, final long id) {
boolean handled = false;
if (mOnItemLongClickListener != null) {
handled = mOnItemLongClickListener.onItemLongClick(this, mDownTouchView, mDownTouchPosition, id);
}
if (!handled) {
mContextMenuInfo = new AdapterContextMenuInfo(view, position, id);
handled = super.showContextMenuForChild(this);
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
// CHECKSTYLE:ON
// CHECKSTYLE:OFF unmodified
private void dispatchPress(final View child) {
if (child != null) {
child.setPressed(true);
}
setPressed(true);
}
private void dispatchUnpress() {
for (int i = getChildCount() - 1; i >= 0; i--) {
getChildAt(i).setPressed(false);
}
setPressed(false);
}
// CHECKSTYLE:OFF unmodified
private void fillToGalleryLeft() {
if (mIsRtl) {
fillToGalleryLeftRtl();
} else {
fillToGalleryLeftLtr();
}
}
private void fillToGalleryLeftLtr() {
final int itemSpacing = mSpacing;
final int galleryLeft = getPaddingLeft();
// Set state for initial iteration
View prevIterationView = getChildAt(0);
int curPosition;
int curRightEdge;
if (prevIterationView != null) {
curPosition = mFirstPosition - 1;
curRightEdge = prevIterationView.getLeft() - itemSpacing;
} else {
// No children available!
curPosition = 0;
curRightEdge = getRight() - getLeft() - getPaddingRight();
mShouldStopFling = true;
}
while (curRightEdge > galleryLeft && curPosition >= 0) {
prevIterationView = makeAndAddView(curPosition, curPosition - mSelectedPosition, curRightEdge, false);
// Remember some state
mFirstPosition = curPosition;
// Set state for next iteration
curRightEdge = prevIterationView.getLeft() - itemSpacing;
curPosition--;
}
}
private void fillToGalleryLeftRtl() {
final int itemSpacing = mSpacing;
final int galleryLeft = getPaddingLeft();
final int numChildren = getChildCount();
// Set state for initial iteration
View prevIterationView = getChildAt(numChildren - 1);
int curPosition;
int curRightEdge;
if (prevIterationView != null) {
curPosition = mFirstPosition + numChildren;
curRightEdge = prevIterationView.getLeft() - itemSpacing;
} else {
// No children available!
mFirstPosition = curPosition = mItemCount - 1;
curRightEdge = getRight() - getLeft() - getPaddingRight();
mShouldStopFling = true;
}
while (curRightEdge > galleryLeft && curPosition < mItemCount) {
prevIterationView = makeAndAddView(curPosition, curPosition - mSelectedPosition, curRightEdge, false);
// Set state for next iteration
curRightEdge = prevIterationView.getLeft() - itemSpacing;
curPosition++;
}
}
// CHECKSTYLE:ON
// Unused methods from GestureDetector.OnGestureListener below
private void fillToGalleryRight() {
if (mIsRtl) {
fillToGalleryRightRtl();
} else {
fillToGalleryRightLtr();
}
}
// Unused methods from GestureDetector.OnGestureListener above
private void fillToGalleryRightLtr() {
final int itemSpacing = mSpacing;
final int galleryRight = getRight() - getLeft() - getPaddingRight();
final int numChildren = getChildCount();
final int numItems = mItemCount;
// Set state for initial iteration
View prevIterationView = getChildAt(numChildren - 1);
int curPosition;
int curLeftEdge;
if (prevIterationView != null) {
curPosition = mFirstPosition + numChildren;
curLeftEdge = prevIterationView.getRight() + itemSpacing;
} else {
mFirstPosition = curPosition = mItemCount - 1;
curLeftEdge = getPaddingLeft();
mShouldStopFling = true;
}
while (curLeftEdge < galleryRight && curPosition < numItems) {
prevIterationView = makeAndAddView(curPosition, curPosition - mSelectedPosition, curLeftEdge, true);
// Set state for next iteration
curLeftEdge = prevIterationView.getRight() + itemSpacing;
curPosition++;
}
}
private void fillToGalleryRightRtl() {
final int itemSpacing = mSpacing;
final int GalleryRight = getRight() - getLeft() - getPaddingRight();
// Set state for initial iteration
View prevIterationView = getChildAt(0);
int curPosition;
int curLeftEdge;
if (prevIterationView != null) {
curPosition = mFirstPosition - 1;
curLeftEdge = prevIterationView.getRight() + itemSpacing;
} else {
curPosition = 0;
curLeftEdge = getPaddingLeft();
mShouldStopFling = true;
}
while (curLeftEdge < GalleryRight && curPosition >= 0) {
prevIterationView = makeAndAddView(curPosition, curPosition - mSelectedPosition, curLeftEdge, true);
// Remember some state
mFirstPosition = curPosition;
// Set state for next iteration
curLeftEdge = prevIterationView.getRight() + itemSpacing;
curPosition--;
}
}
/**
* Modified from getCenterOfGallery. This now gets the left of the view
* (plus padding).
*
* @return The left of this Gallery.
*/
private int getGalleryLockPoint() {
// Lock left of the view
return getPaddingLeft();
}
/**
* Obtain a view, either by pulling an existing view from the recycler or by
* getting a new one from the adapter. If we are animating, make sure there
* is enough information in the view's layout parameters to animate from the
* old to new positions.
*
* @param position Position in the Gallery for the view to obtain
* @param offset Offset from the selected position
* @param x X-coordinate indicating where this view should be placed. This
* will either be the left or right edge of the view, depending
* on the fromLeft parameter
* @param fromLeft Are we positioning views based on the left edge? (i.e.,
* building from left to right)?
* @return A view that has been added to the Gallery
*/
private View makeAndAddView(final int position, final int offset, final int x, final boolean fromLeft) {
View child;
if (!mDataChanged) {
child = mRecycler.get(position);
if (child != null) {
// Can reuse an existing view
final int childLeft = child.getLeft();
// Remember left and right edges of where views have been placed
mRightMost = Math.max(mRightMost, childLeft + child.getMeasuredWidth());
mLeftMost = Math.min(mLeftMost, childLeft);
// Position the view
setUpChild(child, offset, x, fromLeft);
return child;
}
}
// Nothing found in the recycler -- ask the adapter for a view
child = mAdapter.getView(position, null, this);
// Position the view
setUpChild(child, offset, x, fromLeft);
return child;
}
// CHECKSTYLE:ON
/**
* Offset the horizontal location of all children of this view by the
* specified number of pixels.
*
* @param offset the number of pixels to offset
*/
private void offsetChildrenLeftAndRight(final int offset) {
for (int i = getChildCount() - 1; i >= 0; i--) {
getChildAt(i).offsetLeftAndRight(offset);
}
}
// CHECKSTYLE:OFF unmodified
private void onFinishedMovement() {