-
Notifications
You must be signed in to change notification settings - Fork 176
/
BoardView.java
1183 lines (1024 loc) · 44.7 KB
/
BoardView.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
/*
* Copyright 2014 Magnus Woxblom
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.woxthebox.draglistview;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.Scroller;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class BoardView extends HorizontalScrollView implements AutoScroller.AutoScrollListener {
public interface BoardCallback {
boolean canDragItemAtPosition(int column, int row);
boolean canDropItemAtPosition(int oldColumn, int oldRow, int newColumn, int newRow);
boolean canDragColumnAtPosition(int index);
boolean canDropColumnAtPosition(int oldIndex, int newIndex);
}
public interface BoardListener {
void onItemDragStarted(int column, int row);
void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow);
void onItemChangedPosition(int oldColumn, int oldRow, int newColumn, int newRow);
void onItemChangedColumn(int oldColumn, int newColumn);
void onFocusedColumnChanged(int oldColumn, int newColumn);
void onColumnDragStarted(int position);
void onColumnDragChangedPosition(int oldPosition, int newPosition);
void onColumnDragEnded(int fromPosition, int toPosition);
}
public static abstract class BoardListenerAdapter implements BoardListener {
@Override
public void onItemDragStarted(int column, int row) {
}
@Override
public void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow) {
}
@Override
public void onItemChangedPosition(int oldColumn, int oldRow, int newColumn, int newRow) {
}
@Override
public void onItemChangedColumn(int oldColumn, int newColumn) {
}
@Override
public void onFocusedColumnChanged(int oldColumn, int newColumn) {
}
@Override
public void onColumnDragStarted(int position) {
}
@Override
public void onColumnDragChangedPosition(int oldPosition, int newPosition) {
}
@Override
public void onColumnDragEnded(int fromPosition, int toPosition) {
}
}
public enum ColumnSnapPosition {
LEFT, CENTER, RIGHT
}
private static final int SCROLL_ANIMATION_DURATION = 325;
private Scroller mScroller;
private AutoScroller mAutoScroller;
private GestureDetector mGestureDetector;
private FrameLayout mRootLayout;
private LinearLayout mColumnLayout;
private ArrayList<DragItemRecyclerView> mLists = new ArrayList<>();
private ArrayList<View> mHeaders = new ArrayList<>();
private ArrayList<View> mFooters = new ArrayList<>();
private DragItemRecyclerView mCurrentRecyclerView;
private DragItem mDragItem;
private DragItem mDragColumn;
private BoardListener mBoardListener;
private BoardCallback mBoardCallback;
private boolean mSnapToColumnWhenScrolling = true;
private boolean mSnapToColumnWhenDragging = true;
private boolean mSnapToColumnInLandscape = false;
private ColumnSnapPosition mSnapPosition = ColumnSnapPosition.CENTER;
private int mCurrentColumn;
private float mTouchX;
private float mTouchY;
private float mDragColumnStartScrollX;
private int mColumnWidth;
private int mColumnSpacing = 0;
private int mBoardEdge = 0;
private int mDragStartColumn;
private int mDragStartRow;
private boolean mHasLaidOut;
private boolean mDragEnabled = true;
private int mLastDragColumn = NO_POSITION;
private int mLastDragRow = NO_POSITION;
private int mDragColumnStartPosition;
private SavedState mSavedState;
public BoardView(Context context) {
super(context);
}
public BoardView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public BoardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BoardView);
mColumnSpacing = a.getDimensionPixelSize(R.styleable.BoardView_columnSpacing, 0);
mBoardEdge = a.getDimensionPixelSize(R.styleable.BoardView_boardEdges, 0);
a.recycle();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Resources res = getResources();
boolean isPortrait = res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (isPortrait) {
mColumnWidth = (int) (res.getDisplayMetrics().widthPixels * 0.87);
} else {
mColumnWidth = (int) (res.getDisplayMetrics().density * 320);
}
mGestureDetector = new GestureDetector(getContext(), new GestureListener());
mScroller = new Scroller(getContext(), new DecelerateInterpolator(1.1f));
mAutoScroller = new AutoScroller(getContext(), this);
mAutoScroller.setAutoScrollMode(snapToColumnWhenDragging() ? AutoScroller.AutoScrollMode.COLUMN : AutoScroller.AutoScrollMode
.POSITION);
mDragItem = new DragItem(getContext());
mDragColumn = new DragItem(getContext());
mDragColumn.setSnapToTouch(false);
mRootLayout = new FrameLayout(getContext());
mRootLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mColumnLayout = new LinearLayout(getContext());
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
mColumnLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mColumnLayout.setMotionEventSplittingEnabled(false);
mRootLayout.addView(mColumnLayout);
mRootLayout.addView(mDragItem.getDragItemView());
addView(mRootLayout);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
updateBoardSpaces();
// Snap to closes column after first layout.
// This is needed so correct column is scrolled to after a rotation.
if (!mHasLaidOut && mSavedState != null) {
mCurrentColumn = mSavedState.currentColumn;
mSavedState = null;
post(new Runnable() {
@Override
public void run() {
scrollToColumn(mCurrentColumn, false);
}
});
}
mHasLaidOut = true;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mSavedState = ss;
requestLayout();
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
return new SavedState(superState, snapToColumnWhenScrolling() ? mCurrentColumn : getClosestSnapColumn());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean retValue = handleTouchEvent(event);
return retValue || super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean retValue = handleTouchEvent(event);
return retValue || super.onTouchEvent(event);
}
private boolean handleTouchEvent(MotionEvent event) {
if (mLists.size() == 0) {
return false;
}
mTouchX = event.getX();
mTouchY = event.getY();
if (isDragging()) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (!mAutoScroller.isAutoScrolling()) {
updateScrollPosition();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mAutoScroller.stopAutoScroll();
if (isDraggingColumn()) {
endDragColumn();
} else {
mCurrentRecyclerView.onDragEnded();
}
if (snapToColumnWhenScrolling()) {
scrollToColumn(getColumnOfList(mCurrentRecyclerView), true);
}
invalidate();
break;
}
return true;
} else {
if (snapToColumnWhenScrolling() && mGestureDetector.onTouchEvent(event)) {
// A page fling occurred, consume event
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
// View was grabbed during animation
mScroller.forceFinished(true);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (snapToColumnWhenScrolling()) {
scrollToColumn(getClosestSnapColumn(), true);
}
break;
}
return false;
}
}
@Override
public void computeScroll() {
if (!mScroller.isFinished() && mScroller.computeScrollOffset()) {
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
if (getScrollX() != x || getScrollY() != y) {
scrollTo(x, y);
}
// If auto scrolling at the same time as the scroller is running,
// then update the drag item position to prevent stuttering item
if (mAutoScroller.isAutoScrolling() && isDragging()) {
if (isDraggingColumn()) {
mDragColumn.setPosition(mTouchX + getScrollX() - mDragColumnStartScrollX, mTouchY);
} else {
mDragItem.setPosition(getRelativeViewTouchX((View) mCurrentRecyclerView.getParent()), getRelativeViewTouchY(mCurrentRecyclerView));
}
}
ViewCompat.postInvalidateOnAnimation(this);
} else if (!snapToColumnWhenScrolling()) {
super.computeScroll();
}
}
@Override
public void onAutoScrollPositionBy(int dx, int dy) {
if (isDragging()) {
scrollBy(dx, dy);
updateScrollPosition();
} else {
mAutoScroller.stopAutoScroll();
}
}
@Override
public void onAutoScrollColumnBy(int columns) {
if (isDragging()) {
int newColumn = mCurrentColumn + columns;
if (columns != 0 && newColumn >= 0 && newColumn < mLists.size()) {
scrollToColumn(newColumn, true);
}
updateScrollPosition();
} else {
mAutoScroller.stopAutoScroll();
}
}
private void updateScrollPosition() {
if (isDraggingColumn()) {
DragItemRecyclerView currentList = getCurrentRecyclerView(mTouchX + getScrollX());
if (mCurrentRecyclerView != currentList) {
int oldIndex = getColumnOfList(mCurrentRecyclerView);
int newIndex = getColumnOfList(currentList);
if (mBoardCallback == null || mBoardCallback.canDropColumnAtPosition(oldIndex, newIndex)) {
moveColumn(oldIndex, newIndex);
}
}
// Need to subtract with scrollX at the beginning of the column drag because of how drag item position is calculated
mDragColumn.setPosition(mTouchX + getScrollX() - mDragColumnStartScrollX, mTouchY);
} else {
// Updated event to scrollview coordinates
DragItemRecyclerView currentList = getCurrentRecyclerView(mTouchX + getScrollX());
if (mCurrentRecyclerView != currentList) {
int oldColumn = getColumnOfList(mCurrentRecyclerView);
int newColumn = getColumnOfList(currentList);
long itemId = mCurrentRecyclerView.getDragItemId();
// Check if it is ok to drop the item in the new column first
int newPosition = currentList.getDragPositionForY(getRelativeViewTouchY(currentList));
if (mBoardCallback == null || mBoardCallback.canDropItemAtPosition(mDragStartColumn, mDragStartRow, newColumn, newPosition)) {
Object item = mCurrentRecyclerView.removeDragItemAndEnd();
if (item != null) {
mCurrentRecyclerView = currentList;
mCurrentRecyclerView.addDragItemAndStart(getRelativeViewTouchY(mCurrentRecyclerView), item, itemId);
mDragItem.setOffset(((View) mCurrentRecyclerView.getParent()).getLeft(), mCurrentRecyclerView.getTop());
if (mBoardListener != null) {
mBoardListener.onItemChangedColumn(oldColumn, newColumn);
}
}
}
}
// Updated event to list coordinates
mCurrentRecyclerView.onDragging(getRelativeViewTouchX((View) mCurrentRecyclerView.getParent()), getRelativeViewTouchY(mCurrentRecyclerView));
}
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
float scrollEdge = getResources().getDisplayMetrics().widthPixels * (isPortrait ? 0.06f : 0.14f);
if (mTouchX > getWidth() - scrollEdge && getScrollX() < mColumnLayout.getWidth()) {
mAutoScroller.startAutoScroll(AutoScroller.ScrollDirection.LEFT);
} else if (mTouchX < scrollEdge && getScrollX() > 0) {
mAutoScroller.startAutoScroll(AutoScroller.ScrollDirection.RIGHT);
} else {
mAutoScroller.stopAutoScroll();
}
invalidate();
}
private float getRelativeViewTouchX(View view) {
return mTouchX + getScrollX() - view.getLeft();
}
private float getRelativeViewTouchY(View view) {
return mTouchY - view.getTop();
}
private DragItemRecyclerView getCurrentRecyclerView(float x) {
for (DragItemRecyclerView list : mLists) {
View parent = (View) list.getParent();
if (parent.getLeft() <= x && parent.getRight() > x) {
return list;
}
}
return mCurrentRecyclerView;
}
private int getColumnOfList(DragItemRecyclerView list) {
int column = 0;
for (int i = 0; i < mLists.size(); i++) {
RecyclerView tmpList = mLists.get(i);
if (tmpList == list) {
column = i;
}
}
return column;
}
private int getCurrentColumn(float posX) {
for (int i = 0; i < mLists.size(); i++) {
RecyclerView list = mLists.get(i);
View parent = (View) list.getParent();
if (parent.getLeft() <= posX && parent.getRight() > posX) {
return i;
}
}
return 0;
}
private int getClosestSnapColumn() {
int column = 0;
int minDiffX = Integer.MAX_VALUE;
for (int i = 0; i < mLists.size(); i++) {
View listParent = (View) mLists.get(i).getParent();
int diffX = 0;
switch (mSnapPosition) {
case LEFT:
int leftPosX = getScrollX();
diffX = Math.abs(listParent.getLeft() - leftPosX);
break;
case CENTER:
int middlePosX = getScrollX() + getMeasuredWidth() / 2;
diffX = Math.abs(listParent.getLeft() + mColumnWidth / 2 - middlePosX);
break;
case RIGHT:
int rightPosX = getScrollX() + getMeasuredWidth();
diffX = Math.abs(listParent.getRight() - rightPosX);
break;
}
if (diffX < minDiffX) {
minDiffX = diffX;
column = i;
}
}
return column;
}
private boolean snapToColumnWhenScrolling() {
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
return mSnapToColumnWhenScrolling && (isPortrait || mSnapToColumnInLandscape);
}
private boolean snapToColumnWhenDragging() {
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
return mSnapToColumnWhenDragging && (isPortrait || mSnapToColumnInLandscape);
}
private boolean isDraggingColumn() {
return mCurrentRecyclerView != null && mDragColumn.isDragging();
}
private boolean isDragging() {
return mCurrentRecyclerView != null && (mCurrentRecyclerView.isDragging() || isDraggingColumn());
}
public RecyclerView getRecyclerView(int column) {
if (column >= 0 && column < mLists.size()) {
return mLists.get(column);
}
return null;
}
public DragItemAdapter getAdapter(int column) {
if (column >= 0 && column < mLists.size()) {
return (DragItemAdapter) mLists.get(column).getAdapter();
}
return null;
}
public int getItemCount() {
int count = 0;
for (DragItemRecyclerView list : mLists) {
count += list.getAdapter().getItemCount();
}
return count;
}
public int getItemCount(int column) {
if (mLists.size() > column) {
return mLists.get(column).getAdapter().getItemCount();
}
return 0;
}
public int getColumnCount() {
return mLists.size();
}
public View getHeaderView(int column) {
return mHeaders.get(column);
}
public View getFooterView(int column) {
return mFooters.get(column);
}
/**
* @return The index of the column with a specific header. If the header can't be found -1 is returned.
*/
public int getColumnOfHeader(View header) {
for (int i = 0; i < mHeaders.size(); i++) {
if (mHeaders.get(i) == header) {
return i;
}
}
return -1;
}
/**
* @return The index of the column with a specific footer. If the footer can't be found -1 is returned.
*/
public int getColumnOfFooter(View footer) {
for (int i = 0; i < mFooters.size(); i++) {
if (mFooters.get(i) == footer) {
return i;
}
}
return -1;
}
public void removeItem(int column, int row) {
if (!isDragging() && mLists.size() > column && mLists.get(column).getAdapter().getItemCount() > row) {
DragItemAdapter adapter = (DragItemAdapter) mLists.get(column).getAdapter();
adapter.removeItem(row);
}
}
public void addItem(int column, int row, Object item, boolean scrollToItem) {
if (!isDragging() && mLists.size() > column && mLists.get(column).getAdapter().getItemCount() >= row) {
DragItemAdapter adapter = (DragItemAdapter) mLists.get(column).getAdapter();
adapter.addItem(row, item);
if (scrollToItem) {
scrollToItem(column, row, false);
}
}
}
public void moveItem(int fromColumn, int fromRow, int toColumn, int toRow, boolean scrollToItem) {
if (!isDragging() && mLists.size() > fromColumn && mLists.get(fromColumn).getAdapter().getItemCount() > fromRow
&& mLists.size() > toColumn && mLists.get(toColumn).getAdapter().getItemCount() >= toRow) {
DragItemAdapter adapter = (DragItemAdapter) mLists.get(fromColumn).getAdapter();
Object item = adapter.removeItem(fromRow);
adapter = (DragItemAdapter) mLists.get(toColumn).getAdapter();
adapter.addItem(toRow, item);
if (scrollToItem) {
scrollToItem(toColumn, toRow, false);
}
}
}
public void moveItem(long itemId, int toColumn, int toRow, boolean scrollToItem) {
for (int i = 0; i < mLists.size(); i++) {
RecyclerView.Adapter adapter = mLists.get(i).getAdapter();
final int count = adapter.getItemCount();
for (int j = 0; j < count; j++) {
long id = adapter.getItemId(j);
if (id == itemId) {
moveItem(i, j, toColumn, toRow, scrollToItem);
return;
}
}
}
}
public void replaceItem(int column, int row, Object item, boolean scrollToItem) {
if (!isDragging() && mLists.size() > column && mLists.get(column).getAdapter().getItemCount() > row) {
DragItemAdapter adapter = (DragItemAdapter) mLists.get(column).getAdapter();
adapter.removeItem(row);
adapter.addItem(row, item);
if (scrollToItem) {
scrollToItem(column, row, false);
}
}
}
public void scrollToItem(int column, int row, boolean animate) {
if (!isDragging() && mLists.size() > column && mLists.get(column).getAdapter().getItemCount() > row) {
mScroller.forceFinished(true);
scrollToColumn(column, animate);
if (animate) {
mLists.get(column).smoothScrollToPosition(row);
} else {
mLists.get(column).scrollToPosition(row);
}
}
}
public void scrollToColumn(int column, boolean animate) {
if (mLists.size() <= column) {
return;
}
View parent = (View) mLists.get(column).getParent();
MarginLayoutParams parentLayoutParams = (MarginLayoutParams) parent.getLayoutParams();
int newX = 0;
switch (mSnapPosition) {
case LEFT:
newX = parent.getLeft() - parentLayoutParams.leftMargin;
break;
case CENTER:
int indent = (getMeasuredWidth() - parent.getMeasuredWidth() - parentLayoutParams.leftMargin - parentLayoutParams.rightMargin) / 2;
newX = parent.getLeft() - parentLayoutParams.leftMargin - indent;
break;
case RIGHT:
newX = parent.getRight() + parentLayoutParams.rightMargin - getMeasuredWidth();
break;
}
int maxScroll = mRootLayout.getMeasuredWidth() - getMeasuredWidth();
newX = newX < 0 ? 0 : newX;
newX = newX > maxScroll ? maxScroll : newX;
if (getScrollX() != newX) {
mScroller.forceFinished(true);
if (animate) {
mScroller.startScroll(getScrollX(), getScrollY(), newX - getScrollX(), 0, SCROLL_ANIMATION_DURATION);
ViewCompat.postInvalidateOnAnimation(this);
} else {
scrollTo(newX, getScrollY());
}
}
int oldColumn = mCurrentColumn;
mCurrentColumn = column;
if (mBoardListener != null && oldColumn != mCurrentColumn) {
mBoardListener.onFocusedColumnChanged(oldColumn, mCurrentColumn);
}
}
public void clearBoard() {
int count = mLists.size();
for (int i = count - 1; i >= 0; i--) {
mColumnLayout.removeViewAt(i);
mHeaders.remove(i);
mFooters.remove(i);
mLists.remove(i);
}
}
public void removeColumn(int column) {
if (column >= 0 && mLists.size() > column) {
mColumnLayout.removeViewAt(column);
mHeaders.remove(column);
mFooters.remove(column);
mLists.remove(column);
updateBoardSpaces();
}
}
public boolean isDragEnabled() {
return mDragEnabled;
}
public void setDragEnabled(boolean enabled) {
mDragEnabled = enabled;
if (mLists.size() > 0) {
for (DragItemRecyclerView list : mLists) {
list.setDragEnabled(mDragEnabled);
}
}
}
/**
* @return The index of the currently focused column. If column snapping is not enabled this will always return 0.
*/
public int getFocusedColumn() {
if (!snapToColumnWhenScrolling()) {
return 0;
}
return mCurrentColumn;
}
/**
* @param width the width of columns in both portrait and landscape. This must be called before {@link #addColumn} is
* called for the width to take effect.
*/
public void setColumnWidth(int width) {
mColumnWidth = width;
}
/**
* @param columnSpacing the interval between columns in pixels.
*/
public void setColumnSpacing(int columnSpacing) {
mColumnSpacing = columnSpacing;
updateBoardSpaces();
}
/**
* @param boardEdge the extra space in pixels on the start and on the end of columns.
*/
public void setBoardEdge(int boardEdge) {
mBoardEdge = boardEdge;
updateBoardSpaces();
}
/**
* @param snapToColumn true if scrolling should snap to columns. Only applies to portrait mode.
*/
public void setSnapToColumnsWhenScrolling(boolean snapToColumn) {
mSnapToColumnWhenScrolling = snapToColumn;
}
/**
* @param snapToColumn true if dragging should snap to columns when dragging towards the edge. Only applies to portrait mode.
*/
public void setSnapToColumnWhenDragging(boolean snapToColumn) {
mSnapToColumnWhenDragging = snapToColumn;
mAutoScroller.setAutoScrollMode(snapToColumnWhenDragging() ? AutoScroller.AutoScrollMode.COLUMN : AutoScroller.AutoScrollMode
.POSITION);
}
/**
* @param snapToColumnInLandscape true if dragging should snap to columns when dragging towards the edge also in landscape mode.
*/
public void setSnapToColumnInLandscape(boolean snapToColumnInLandscape) {
mSnapToColumnInLandscape = snapToColumnInLandscape;
mAutoScroller.setAutoScrollMode(snapToColumnWhenDragging() ? AutoScroller.AutoScrollMode.COLUMN : AutoScroller.AutoScrollMode
.POSITION);
}
/**
* @param snapPosition determines what position a column will snap to. LEFT, CENTER or RIGHT.
*/
public void setColumnSnapPosition(ColumnSnapPosition snapPosition) {
mSnapPosition = snapPosition;
}
/**
* @param snapToTouch true if the drag item should snap to touch position when a drag is started.
*/
public void setSnapDragItemToTouch(boolean snapToTouch) {
mDragItem.setSnapToTouch(snapToTouch);
}
public void setBoardListener(BoardListener listener) {
mBoardListener = listener;
}
public void setBoardCallback(BoardCallback callback) {
mBoardCallback = callback;
}
/**
* Set a custom drag item to control the visuals and animations when dragging a list item.
*/
public void setCustomDragItem(DragItem dragItem) {
DragItem newDragItem = dragItem != null ? dragItem : new DragItem(getContext());
if (dragItem == null) {
newDragItem.setSnapToTouch(true);
}
mDragItem = newDragItem;
mRootLayout.removeViewAt(1);
mRootLayout.addView(mDragItem.getDragItemView());
}
/**
* Set a custom drag item to control the visuals and animations when dragging a column.
*/
public void setCustomColumnDragItem(DragItem dragItem) {
DragItem newDragItem = dragItem != null ? dragItem : new DragItem(getContext());
if (dragItem == null) {
newDragItem.setSnapToTouch(false);
}
mDragColumn = newDragItem;
}
private void startDragColumn(DragItemRecyclerView recyclerView, float posX, float posY) {
mDragColumnStartScrollX = getScrollX();
mCurrentRecyclerView = recyclerView;
View columnView = mColumnLayout.getChildAt(getColumnOfList(recyclerView));
mDragColumn.startDrag(columnView, posX, posY);
mRootLayout.addView(mDragColumn.getDragItemView());
columnView.setAlpha(0);
if (mBoardListener != null) {
mDragColumnStartPosition = getColumnOfList(mCurrentRecyclerView);
mBoardListener.onColumnDragStarted(mDragColumnStartPosition);
}
}
private void endDragColumn() {
mDragColumn.endDrag(mDragColumn.getRealDragView(), new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mDragColumn.getRealDragView().setAlpha(1);
mDragColumn.hide();
mRootLayout.removeView(mDragColumn.getDragItemView());
if (mBoardListener != null) {
mBoardListener.onColumnDragEnded(mDragColumnStartPosition,
getColumnOfList(mCurrentRecyclerView));
}
}
});
}
private void moveColumn(final int fromIndex, final int toIndex) {
DragItemRecyclerView list = mLists.remove(fromIndex);
mLists.add(toIndex, list);
View header = mHeaders.remove(fromIndex);
mHeaders.add(toIndex, header);
View footer = mFooters.remove(fromIndex);
mFooters.add(toIndex, footer);
final View column1 = mColumnLayout.getChildAt(fromIndex);
final View column2 = mColumnLayout.getChildAt(toIndex);
mColumnLayout.removeViewAt(fromIndex);
mColumnLayout.addView(column1, toIndex);
updateBoardSpaces();
mColumnLayout.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mColumnLayout.removeOnLayoutChangeListener(this);
column2.setTranslationX(column2.getTranslationX() + column1.getLeft() - column2.getLeft());
column2.animate().translationX(0).setDuration(350).start();
}
});
if (mBoardListener != null) {
mBoardListener.onColumnDragChangedPosition(fromIndex, toIndex);
}
}
/**
* Inserts a column to the board at a specific index.
*
* @param adapter Adapter with the items for the column.
* @param index Index where on the board to add the column.
* @param header Header view that will be positioned above the column. Can be null.
* @param columnDragView View that will act as handle to drag and drop columns. Can be null.
* @param hasFixedItemSize If the items will have a fixed or dynamic size.
*
* @return The created DragItemRecyclerView.
*
* @deprecated use {@link #insertColumn(int, ColumnProperties)}
*/
public DragItemRecyclerView insertColumn(final DragItemAdapter adapter, int index, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize) {
return insertColumn(adapter, index, header, columnDragView, hasFixedItemSize, new LinearLayoutManager(getContext()));
}
/**
*
* @deprecated use {@link #insertColumn(int, ColumnProperties)}
*/
public DragItemRecyclerView insertColumn(final DragItemAdapter adapter, int index, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize, @NonNull RecyclerView.LayoutManager layoutManager) {
ColumnProperties columnProperties = ColumnProperties.Builder.newBuilder(adapter)
.setHeader(header)
.setColumnDragView(columnDragView)
.setHasFixedItemSize(hasFixedItemSize)
.setLayoutManager(layoutManager)
.build();
return addColumnTo(index, columnProperties);
}
/**
* Inserts a column to the board at a specific index.
*
* @param index Index where on the board to add the column.
* @param columnProperties Properties of the column to be added.
*/
public void insertColumn(int index, @NonNull ColumnProperties columnProperties) {
addColumnTo(index, columnProperties);
}
/**
* Adds a column at the last index of the board.
*
* @param adapter Adapter with the items for the column.
* @param header Header view that will be positioned above the column. Can be null.
* @param columnDragView View that will act as handle to drag and drop columns. Can be null.
* @param hasFixedItemSize If the items will have a fixed or dynamic size.
*
* @return The created DragItemRecyclerView.
*
* @deprecated use {@link #addColumn(ColumnProperties)}
*/
public DragItemRecyclerView addColumn(final DragItemAdapter adapter, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize) {
return addColumn(adapter, header, columnDragView, hasFixedItemSize, new LinearLayoutManager(getContext()));
}
/**
* @deprecated use {@link #addColumn(ColumnProperties)}
*/
public DragItemRecyclerView addColumn(final DragItemAdapter adapter, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize, @NonNull RecyclerView.LayoutManager layoutManager) {
ColumnProperties columnProperties = ColumnProperties.Builder.newBuilder(adapter)
.setHeader(header)
.setColumnDragView(columnDragView)
.setHasFixedItemSize(hasFixedItemSize)
.setLayoutManager(layoutManager)
.build();
return addColumnTo(getColumnCount(), columnProperties);
}
/**
* Adds a column at the last index of the board.
*
* @param columnProperties Properties of the column to be added.
*/
public void addColumn(@NonNull ColumnProperties columnProperties) {
addColumnTo(getColumnCount(), columnProperties);
}
private DragItemRecyclerView addColumnTo(int index, @NonNull ColumnProperties columnProperties) {
if (index > getColumnCount()) {
throw new IllegalArgumentException("Index is out of bounds");
}
final DragItemRecyclerView recyclerView = (DragItemRecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.drag_item_recycler_view, this, false);
recyclerView.setId(getColumnCount());
recyclerView.setHorizontalScrollBarEnabled(false);
recyclerView.setVerticalScrollBarEnabled(false);
recyclerView.setMotionEventSplittingEnabled(false);
recyclerView.setDragItem(mDragItem);
recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
RecyclerView.LayoutManager layoutManager = columnProperties.getLayoutManager();
recyclerView.setLayoutManager(layoutManager != null ? layoutManager : new LinearLayoutManager(getContext()));
recyclerView.setBackgroundColor(columnProperties.getItemsSectionBackgroundColor());
recyclerView.setHasFixedSize(columnProperties.hasFixedItemSize());
List<RecyclerView.ItemDecoration> itemDecorations = columnProperties.getItemDecorations();
for (int i = 0; i < itemDecorations.size(); i++) {
recyclerView.addItemDecoration(itemDecorations.get(i));
}
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setDragItemListener(new DragItemRecyclerView.DragItemListener() {
@Override
public void onDragStarted(int itemPosition, float x, float y) {
mDragStartColumn = getColumnOfList(recyclerView);
mDragStartRow = itemPosition;
mCurrentRecyclerView = recyclerView;
mDragItem.setOffset(((View) mCurrentRecyclerView.getParent()).getX(), mCurrentRecyclerView.getY());
if (mBoardListener != null) {
mBoardListener.onItemDragStarted(mDragStartColumn, mDragStartRow);
}
invalidate();
}
@Override
public void onDragging(int itemPosition, float x, float y) {
int column = getColumnOfList(recyclerView);
boolean positionChanged = column != mLastDragColumn || itemPosition != mLastDragRow;
if (mBoardListener != null && positionChanged) {
mLastDragColumn = column;
mLastDragRow = itemPosition;
mBoardListener.onItemChangedPosition(mDragStartColumn, mDragStartRow, column, itemPosition);
}
}
@Override
public void onDragEnded(int newItemPosition) {
mLastDragColumn = NO_POSITION;
mLastDragRow = NO_POSITION;
if (mBoardListener != null) {
mBoardListener.onItemDragEnded(mDragStartColumn, mDragStartRow, getColumnOfList(recyclerView), newItemPosition);
}