forked from Comcast/FreeFlow
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFreeFlowContainer.java
More file actions
1954 lines (1616 loc) · 51.3 KB
/
FreeFlowContainer.java
File metadata and controls
1954 lines (1616 loc) · 51.3 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 2013 Comcast Cable Communications Management, LLC
*
* 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 com.comcast.freeflow.core;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import org.freeflow.BuildConfig;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v4.util.SimpleArrayMap;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.ActionMode;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Checkable;
import android.widget.EdgeEffect;
import android.widget.OverScroller;
import com.comcast.freeflow.animations.DefaultLayoutAnimator;
import com.comcast.freeflow.animations.FreeFlowLayoutAnimator;
import com.comcast.freeflow.layouts.FreeFlowLayout;
import com.comcast.freeflow.utils.ViewUtils;
public class FreeFlowContainer extends AbsLayoutContainer {
private static final String TAG = "Container";
// ViewPool class
protected ViewPool viewpool;
// Not used yet, but we'll probably need to
// prevent layout in <code>layout()</code> method
private boolean preventLayout = false;
protected SectionedAdapter mAdapter;
protected FreeFlowLayout mLayout;
/**
* The X position of the active ViewPort
*/
protected int viewPortX = 0;
/**
* The Y position of the active ViewPort
*/
protected int viewPortY = 0;
/**
* The scrollable width in pixels. This is usually computed as the
* difference between the width of the container and the contentWidth as
* computed by the layout.
*/
protected int mScrollableWidth;
/**
* The scrollable height in pixels. This is usually computed as the
* difference between the height of the container and the contentHeight as
* computed by the layout.
*/
protected int mScrollableHeight;
private VelocityTracker mVelocityTracker = null;
private float deltaX = -1f;
private float deltaY = -1f;
private int maxFlingVelocity;
private int minFlingVelocity;
private int overflingDistance;
/*private int overscrollDistance;*/
private int touchSlop;
private Runnable mTouchModeReset;
private Runnable mPerformClick;
private Runnable mPendingCheckForTap;
private Runnable mPendingCheckForLongPress;
private OverScroller scroller;
protected EdgeEffect mLeftEdge, mRightEdge, mTopEdge, mBottomEdge;
private ArrayList<OnScrollListener> scrollListeners = new ArrayList<FreeFlowContainer.OnScrollListener>();
// This flag controls whether onTap/onLongPress/onTouch trigger
// the ActionMode
// private boolean mDataChanged = false;
/**
* TODO: ContextMenu action on long press has not been implemented yet
*/
protected ContextMenuInfo mContextMenuInfo = null;
/**
* Holds the checked items when the Container is in CHOICE_MODE_MULTIPLE
*/
protected SimpleArrayMap<IndexPath, Boolean> mCheckStates = null;
ActionMode mChoiceActionMode;
/**
* Wraps the callback for MultiChoiceMode
*/
MultiChoiceModeWrapper mMultiChoiceModeCallback;
/**
* Normal list that does not indicate choices
*/
public static final int CHOICE_MODE_NONE = 0;
/**
* The list allows up to one choice
*/
public static final int CHOICE_MODE_SINGLE = 1;
/**
* The list allows multiple choices
*/
public static final int CHOICE_MODE_MULTIPLE = 2;
/**
* The list allows multiple choices in a modal selection mode
*/
public static final int CHOICE_MODE_MULTIPLE_MODAL = 3;
/**
* The value of the current ChoiceMode
*
* @see <a href=
* "http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode"
* >List View's Choice Mode</a>
*/
int mChoiceMode = CHOICE_MODE_NONE;
private LayoutParams params = new LayoutParams(0, 0);
private FreeFlowLayoutAnimator layoutAnimator = new DefaultLayoutAnimator();
private FreeFlowItem beginTouchAt;
private boolean markLayoutDirty = false;
private boolean markAdapterDirty = false;
/**
* When Layout is computed, should scroll positions be recalculated? When a
* new layout is set, the Container can try to make sure an item that was
* visible in one layout is also visible in the new layout. However when
* data is just invalidated and additional data is loaded, you don't want
* the Viewport to be jumping around.
*/
private boolean shouldRecalculateScrollWhenComputingLayout = true;
private FreeFlowLayout oldLayout;
private OnTouchModeChangedListener mOnTouchModeChangedListener;
public void setOnTouchModeChangedListener(
OnTouchModeChangedListener onTouchModeChangedListener) {
mOnTouchModeChangedListener = onTouchModeChangedListener;
}
public FreeFlowContainer(Context context) {
super(context);
}
public FreeFlowContainer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FreeFlowContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void init(Context context) {
viewpool = new ViewPool();
frames = new LinkedHashMap<Object, FreeFlowItem>();
ViewConfiguration configuration = ViewConfiguration.get(context);
maxFlingVelocity = configuration.getScaledMaximumFlingVelocity();
minFlingVelocity = configuration.getScaledMinimumFlingVelocity();
overflingDistance = configuration.getScaledOverflingDistance();
/*overscrollDistance = configuration.getScaledOverscrollDistance();*/
touchSlop = configuration.getScaledTouchSlop();
scroller = new OverScroller(context);
setEdgeEffectsEnabled(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
logLifecycleEvent(" onMeasure ");
int beforeWidth = getWidth();
int beforeHeight = getHeight();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int afterWidth = MeasureSpec.getSize(widthMeasureSpec);
int afterHeight = MeasureSpec.getSize(heightMeasureSpec);
// TODO: prepareLayout should at some point take sizeChanged as a param
// to not
// avoidable calculations
boolean sizeChanged = (beforeHeight == afterHeight)
&& (beforeWidth == afterWidth);
if (this.mLayout != null) {
mLayout.setDimensions(afterWidth, afterHeight);
}
if (mLayout == null || mAdapter == null) {
logLifecycleEvent("Nothing to do: returning");
return;
}
if (markAdapterDirty || markLayoutDirty) {
computeLayout(afterWidth, afterHeight);
}
if (dataSetChanged) {
dataSetChanged = false;
for (FreeFlowItem item : frames.values()) {
if (item.itemIndex >= 0 && item.itemSection >= 0) {
mAdapter.getItemView(item.itemSection, item.itemIndex,
item.view, this);
}
}
}
}
protected boolean dataSetChanged = false;
/**
* Notifies the attached observers that the underlying data has been changed
* and any View reflecting the data set should refresh itself.
*/
public void notifyDataSetChanged() {
dataSetChanged = true;
requestLayout();
}
/**
* @deprecated Use dataInvalidated(boolean shouldRecalculateScrollPositions)
* instead
*/
public void dataInvalidated() {
dataInvalidated(false);
}
/**
* Called to inform the Container that the underlying data on the adapter
* has changed (more items added/removed). Note that this won't update the
* views if the adapter's data objects are the same but the values in those
* objects have changed. To update those call {@code notifyDataSetChanged}
*
* @param shouldRecalculateScrollPositions
*/
public void dataInvalidated(boolean shouldRecalculateScrollPositions) {
logLifecycleEvent("Data Invalidated");
if (mLayout == null || mAdapter == null) {
return;
}
shouldRecalculateScrollWhenComputingLayout = shouldRecalculateScrollPositions;
markAdapterDirty = true;
requestLayout();
}
/**
* The heart of the system. Calls the layout to get the frames needed,
* decides which view should be kept in focus if view transitions are going
* to happen and then kicks off animation changes if things have changed
*
* @param w
* Width of the viewport. Since right now we don't support
* margins and padding, this is width of the container.
* @param h
* Height of the viewport. Since right now we don't support
* margins and padding, this is height of the container.
*/
protected void computeLayout(int w, int h) {
markLayoutDirty = false;
markAdapterDirty = false;
mLayout.prepareLayout();
if (shouldRecalculateScrollWhenComputingLayout) {
computeViewPort(mLayout);
}
Map<Object, FreeFlowItem> oldFrames = frames;
frames = new LinkedHashMap<Object, FreeFlowItem>();
copyFrames(mLayout.getItemProxies(viewPortX, viewPortY), frames);
// Create a copy of the incoming values because the source
// layout may change the map inside its own class
dispatchLayoutComputed();
animateChanges(getViewChanges(oldFrames, frames));
}
/**
* Copies the frames from one LinkedHashMap into another. The items are
* cloned cause we modify the rectangles of the items as they are moving
*/
protected void copyFrames(Map<Object, FreeFlowItem> srcFrames,
Map<Object, FreeFlowItem> destFrames) {
Iterator<?> it = srcFrames.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) it.next();
FreeFlowItem pr = (FreeFlowItem) pairs.getValue();
pr = FreeFlowItem.clone(pr);
destFrames.put(pairs.getKey(), pr);
}
}
/**
* Adds a view based on the current viewport. If we can get a view from the
* ViewPool, we dont need to construct a new instance, else we will based on
* the View class returned by the <code>Adapter</code>
*
* @param freeflowItem
* <code>FreeFlowItem</code> instance that determines the View
* being positioned
*/
protected void addAndMeasureViewIfNeeded(FreeFlowItem freeflowItem) {
View view;
if (freeflowItem.view == null) {
View convertView = viewpool.getViewFromPool(mAdapter
.getViewType(freeflowItem));
if (freeflowItem.isHeader) {
view = mAdapter.getHeaderViewForSection(
freeflowItem.itemSection, convertView, this);
} else {
view = mAdapter.getItemView(freeflowItem.itemSection,
freeflowItem.itemIndex, convertView, this);
}
if (view instanceof FreeFlowContainer)
throw new IllegalStateException(
"A container cannot be a direct child view to a container");
freeflowItem.view = view;
prepareViewForAddition(view, freeflowItem);
addView(view, getChildCount(), params);
}
view = freeflowItem.view;
int widthSpec = MeasureSpec.makeMeasureSpec(freeflowItem.frame.width(),
MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(
freeflowItem.frame.height(), MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
}
/**
* Does all the necessary work right before a view is about to be laid out.
*
* @param view
* The View that will be added to the Container
* @param freeflowItem
* The <code>FreeFlowItem</code> instance that represents the
* view that will be positioned
*/
protected void prepareViewForAddition(View view, FreeFlowItem freeflowItem) {
if (view instanceof Checkable) {
((Checkable) view).setChecked(isChecked(freeflowItem.itemSection,
freeflowItem.itemIndex));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
logLifecycleEvent("onLayout");
dispatchLayoutComplete(isAnimatingChanges);
// mDataChanged = false;
}
protected void doLayout(FreeFlowItem freeflowItem) {
View view = freeflowItem.view;
Rect frame = freeflowItem.frame;
view.layout(frame.left - viewPortX, frame.top - viewPortY, frame.right
- viewPortX, frame.bottom - viewPortY);
}
/**
* Sets the layout on the Container. If a previous layout was already
* applied, this causes the views to animate to the new layout positions.
* Scroll positions will also be reset.
*
* @see FreeFlowLayout
* @param newLayout
*/
public void setLayout(FreeFlowLayout newLayout) {
if (newLayout == mLayout || newLayout == null) {
return;
}
stopScrolling();
oldLayout = mLayout;
mLayout = newLayout;
shouldRecalculateScrollWhenComputingLayout = true;
if (mAdapter != null) {
mLayout.setAdapter(mAdapter);
}
dispatchLayoutChanging(oldLayout, newLayout);
markLayoutDirty = true;
viewPortX = 0;
viewPortY = 0;
logLifecycleEvent("Setting layout");
requestLayout();
}
/**
* Stops the scrolling immediately
*/
public void stopScrolling() {
if (!scroller.isFinished()) {
scroller.forceFinished(true);
}
removeCallbacks(flingRunnable);
resetAllCallbacks();
mTouchMode = TOUCH_MODE_REST;
if (mOnTouchModeChangedListener != null) {
mOnTouchModeChangedListener.onTouchModeChanged(mTouchMode);
}
}
/**
* Resets all Runnables that are checking on various statuses
*/
protected void resetAllCallbacks() {
if (mPendingCheckForTap != null) {
removeCallbacks(mPendingCheckForTap);
mPendingCheckForTap = null;
}
if (mPendingCheckForLongPress != null) {
removeCallbacks(mPendingCheckForLongPress);
mPendingCheckForLongPress = null;
}
if (mTouchModeReset != null) {
removeCallbacks(mTouchModeReset);
mTouchModeReset = null;
}
if (mPerformClick != null) {
removeCallbacks(mPerformClick);
mPerformClick = null;
}
}
/**
* @return The layout currently applied to the Container
*/
public FreeFlowLayout getLayout() {
return mLayout;
}
/**
* Computes the Rectangle that defines the ViewPort. The Container tries to
* keep the view at the top left of the old layout visible in the new
* layout.
*
* @see getViewportTop
* @see getViewportLeft
*
*/
protected void computeViewPort(FreeFlowLayout newLayout) {
if (mLayout == null || frames == null || frames.size() == 0) {
viewPortX = 0;
viewPortY = 0;
return;
}
Object data = null;
int lowestSection = Integer.MAX_VALUE;
int lowestPosition = Integer.MAX_VALUE;
// Find the frame of of the first item in the first section in the
// current set of frames defining the viewport
// Changing layout will then keep this item in the viewport of the new
// layout
// TODO: Need to make sure this item is actually being shown in the
// viewport and not just in some offscreen buffer
for (FreeFlowItem fd : frames.values()) {
if (fd.itemSection < lowestSection
|| (fd.itemSection == lowestSection && fd.itemIndex < lowestPosition)) {
data = fd.data;
lowestSection = fd.itemSection;
lowestPosition = fd.itemIndex;
}
}
FreeFlowItem freeflowItem = newLayout.getFreeFlowItemForItem(data);
freeflowItem = FreeFlowItem.clone(freeflowItem);
if (freeflowItem == null) {
viewPortX = 0;
viewPortY = 0;
return;
}
Rect vpFrame = freeflowItem.frame;
viewPortX = vpFrame.left;
viewPortY = vpFrame.top;
mScrollableWidth = mLayout.getContentWidth() - getWidth();
mScrollableHeight = mLayout.getContentHeight() - getHeight();
if (mScrollableWidth < 0) {
mScrollableWidth = 0;
}
if (mScrollableHeight < 0) {
mScrollableHeight = 0;
}
if (viewPortX > mScrollableWidth)
viewPortX = mScrollableWidth;
if (viewPortY > mScrollableHeight)
viewPortY = mScrollableHeight;
}
/**
* Returns the actual frame for a view as its on stage. The FreeFlowItem's
* frame object always represents the position it wants to be in but actual
* frame may be different based on animation etc.
*
* @param freeflowItem
* The freeflowItem to get the <code>Frame</code> for
* @return The Frame for the freeflowItem or null if that view doesn't exist
*/
public Rect getActualFrame(final FreeFlowItem freeflowItem) {
View v = freeflowItem.view;
if (v == null) {
return null;
}
Rect of = new Rect();
of.left = (int) (v.getLeft() + v.getTranslationX());
of.top = (int) (v.getTop() + v.getTranslationY());
of.right = (int) (v.getRight() + v.getTranslationX());
of.bottom = (int) (v.getBottom() + v.getTranslationY());
return of;
}
/**
* Returns the <code>FreeFlowItem</code> representing the data passed in IF
* that item is being rendered in the Container.
*
* @param dataItem
* The data object being rendered in a View managed by the
* Container, null otherwise
* @return
*/
public FreeFlowItem getFreeFlowItem(Object dataItem) {
for (FreeFlowItem item : frames.values()) {
if (item.data.equals(dataItem)) {
return item;
}
}
return null;
}
/**
* TODO: This should be renamed to layoutInvalidated, since the layout isn't
* changed
*/
public void layoutChanged() {
logLifecycleEvent("layoutChanged");
markLayoutDirty = true;
dispatchDataChanged();
requestLayout();
}
protected boolean isAnimatingChanges = false;
private void animateChanges(LayoutChangeset changeSet) {
logLifecycleEvent("animating changes: " + changeSet.toString());
if (changeSet.added.size() == 0 && changeSet.removed.size() == 0
&& changeSet.moved.size() == 0) {
return;
}
for (FreeFlowItem freeflowItem : changeSet.getAdded()) {
addAndMeasureViewIfNeeded(freeflowItem);
doLayout(freeflowItem);
}
if (isAnimatingChanges) {
layoutAnimator.cancel();
}
isAnimatingChanges = true;
dispatchAnimationsStarting();
layoutAnimator.animateChanges(changeSet, this);
}
/**
* This method is called by the <code>LayoutAnimator</code> instance once
* all transition animations have been completed.
*
* @param anim
* The LayoutAnimator instance that reported change complete.
*/
public void onLayoutChangeAnimationsCompleted(FreeFlowLayoutAnimator anim) {
// preventLayout = false;
isAnimatingChanges = false;
logLifecycleEvent("layout change animations complete");
for (FreeFlowItem freeflowItem : anim.getChangeSet().getRemoved()) {
View v = freeflowItem.view;
removeView(v);
returnItemToPoolIfNeeded(freeflowItem);
}
dispatchLayoutChangeAnimationsComplete();
// changeSet = null;
}
public LayoutChangeset getViewChanges(Map<Object, FreeFlowItem> oldFrames,
Map<Object, FreeFlowItem> newFrames) {
return getViewChanges(oldFrames, newFrames, false);
}
public LayoutChangeset getViewChanges(Map<Object, FreeFlowItem> oldFrames,
Map<Object, FreeFlowItem> newFrames, boolean moveEvenIfSame) {
// cleanupViews();
LayoutChangeset change = new LayoutChangeset();
if (oldFrames == null) {
markAdapterDirty = false;
for (FreeFlowItem freeflowItem : newFrames.values()) {
change.addToAdded(freeflowItem);
}
return change;
}
if (markAdapterDirty) {
markAdapterDirty = false;
for (FreeFlowItem freeflowItem : newFrames.values()) {
change.addToAdded(freeflowItem);
}
for (FreeFlowItem freeflowItem : oldFrames.values()) {
change.addToDeleted(freeflowItem);
}
return change;
}
Iterator<?> it = newFrames.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> m = (Map.Entry<?, ?>) it.next();
FreeFlowItem freeflowItem = (FreeFlowItem) m.getValue();
if (oldFrames.get(m.getKey()) != null) {
FreeFlowItem old = oldFrames.remove(m.getKey());
freeflowItem.view = old.view;
// if (moveEvenIfSame || !old.compareRect(((FreeFlowItem)
// m.getValue()).frame)) {
if (moveEvenIfSame
|| !old.frame
.equals(((FreeFlowItem) m.getValue()).frame)) {
change.addToMoved(freeflowItem,
getActualFrame(freeflowItem));
}
} else {
change.addToAdded(freeflowItem);
}
}
for (FreeFlowItem freeflowItem : oldFrames.values()) {
change.addToDeleted(freeflowItem);
}
frames = newFrames;
return change;
}
@Override
public void requestLayout() {
if (!preventLayout) {
/**
* Ends up with a call to <code>onMeasure</code> where all the logic
* lives
*/
super.requestLayout();
}
}
/**
* Sets the adapter for the this CollectionView.All view pools will be
* cleared at this point and all views on the stage will be cleared
*
* @param adapter
* The {@link SectionedAdapter} that will populate this
* Collection
*/
public void setAdapter(SectionedAdapter adapter) {
if (adapter == mAdapter) {
return;
}
stopScrolling();
logLifecycleEvent("setting adapter");
markAdapterDirty = true;
viewPortX = 0;
viewPortY = 0;
shouldRecalculateScrollWhenComputingLayout = true;
this.mAdapter = adapter;
if (adapter != null) {
viewpool.initializeViewPool(adapter.getViewTypes());
}
if (mLayout != null) {
mLayout.setAdapter(mAdapter);
}
requestLayout();
}
public FreeFlowLayout getLayoutController() {
return mLayout;
}
/**
* The Viewport defines the rectangular "window" that the container is
* actually showing of the entire view.
*
* @return The left (x) of the viewport within the entire container
*/
public int getViewportLeft() {
return viewPortX;
}
/**
* The Viewport defines the rectangular "window" that the container is
* actually showing of the entire view.
*
* @return The top (y) of the viewport within the entire container
*
*/
public int getViewportTop() {
return viewPortY;
}
/**
* Indicates that we are not in the middle of a touch gesture
*/
public static final int TOUCH_MODE_REST = -1;
/**
* Indicates we just received the touch event and we are waiting to see if
* the it is a tap or a scroll gesture.
*/
public static final int TOUCH_MODE_DOWN = 0;
/**
* Indicates the touch has been recognized as a tap and we are now waiting
* to see if the touch is a longpress
*/
public static final int TOUCH_MODE_TAP = 1;
/**
* Indicates we have waited for everything we can wait for, but the user's
* finger is still down
*/
public static final int TOUCH_MODE_DONE_WAITING = 2;
/**
* Indicates the touch gesture is a scroll
*/
public static final int TOUCH_MODE_SCROLL = 3;
/**
* Indicates the view is in the process of being flung
*/
public static final int TOUCH_MODE_FLING = 4;
/**
* Indicates the touch gesture is an overscroll - a scroll beyond the
* beginning or end.
*/
public static final int TOUCH_MODE_OVERSCROLL = 5;
/**
* Indicates the view is being flung outside of normal content bounds and
* will spring back.
*/
public static final int TOUCH_MODE_OVERFLING = 6;
/**
* One of TOUCH_MODE_REST, TOUCH_MODE_DOWN, TOUCH_MODE_TAP,
* TOUCH_MODE_SCROLL, or TOUCH_MODE_DONE_WAITING
*/
int mTouchMode = TOUCH_MODE_REST;
/**
* The duration for which the scroller will wait before deciding whether the
* user was actually trying to stop the scroll or swuipe again to increase
* the velocity
*/
protected final int FLYWHEEL_TIMEOUT = 40;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (mLayout == null) {
return false;
}
// flag to check if laid out items are wide or tall enough
// to require scrolling
boolean canScroll = false;
if (mLayout.horizontalScrollEnabled()
&& this.mLayout.getContentWidth() > getWidth()) {
canScroll = true;
}
if (mLayout.verticalScrollEnabled()
&& mLayout.getContentHeight() > getHeight()) {
canScroll = true;
}
switch (event.getAction()) {
case (MotionEvent.ACTION_DOWN):
touchDown(event);
break;
case (MotionEvent.ACTION_MOVE):
if (canScroll) {
touchMove(event);
}
break;
case (MotionEvent.ACTION_UP):
touchUp(event);
break;
case (MotionEvent.ACTION_CANCEL):
touchCancel(event);
break;
}
if (!canScroll) {
return true;
}
if (mVelocityTracker == null && canScroll) {
mVelocityTracker = VelocityTracker.obtain();
}
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
}
return true;
}
protected void touchDown(MotionEvent event) {
if(isAnimatingChanges){
layoutAnimator.onContainerTouchDown(event);
}
/*
* Recompute this just to be safe. TODO: We should optimize this to be
* only calculated when a data or layout change happens
*/
mScrollableHeight = mLayout.getContentHeight() - getHeight();
mScrollableWidth = mLayout.getContentWidth() - getWidth();
if (mTouchMode == TOUCH_MODE_FLING) {
// Wait for some time to see if the user is just trying
// to speed up the scroll
postDelayed(new Runnable() {
@Override
public void run() {
if (mTouchMode == TOUCH_MODE_DOWN) {
if (mTouchMode == TOUCH_MODE_DOWN) {
scroller.forceFinished(true);
}
}
}
}, FLYWHEEL_TIMEOUT);
}
beginTouchAt = ViewUtils.getItemAt(frames,
(int) (viewPortX + event.getX()),
(int) (viewPortY + event.getY()));
deltaX = event.getX();
deltaY = event.getY();
mTouchMode = TOUCH_MODE_DOWN;
if (mOnTouchModeChangedListener != null) {
mOnTouchModeChangedListener.onTouchModeChanged(mTouchMode);
}
if (mPendingCheckForTap != null) {
removeCallbacks(mPendingCheckForTap);
mPendingCheckForLongPress = null;
}
if (beginTouchAt != null) {
mPendingCheckForTap = new CheckForTap();
}
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
}
protected void touchMove(MotionEvent event) {
float xDiff = event.getX() - deltaX;
float yDiff = event.getY() - deltaY;
double distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
if (mLayout.verticalScrollEnabled()) {
if (yDiff > 0 && viewPortY == 0) {
if (mEdgeEffectsEnabled) {
float str = (float) distance / getHeight();
mTopEdge.onPull(str);
invalidate();
}
return;
}
if (yDiff < 0 && viewPortY == mScrollableHeight) {
if (mEdgeEffectsEnabled) {
float str = (float) distance / getHeight();
mBottomEdge.onPull(str);
invalidate();
}
return;
}
}
if (mLayout.horizontalScrollEnabled()) {
if (xDiff > 0 && viewPortX == 0) {
if (mEdgeEffectsEnabled) {
float str = (float) distance / getWidth();
mLeftEdge.onPull(str);
invalidate();
}
return;
}
if (xDiff < 0 && viewPortY == mScrollableWidth) {
if (mEdgeEffectsEnabled) {
float str = (float) distance / getWidth();
mRightEdge.onPull(str);