Skip to content

Commit

Permalink
Added support to be able to control drag and drop positions in BoardView
Browse files Browse the repository at this point in the history
  • Loading branch information
woxblom committed Aug 31, 2017
1 parent 2910baa commit cb2bc9a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 33 deletions.
53 changes: 33 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Youtube demo video<br>
}

dependencies {
compile 'com.github.woxthebox:draglistview:1.4.7'
compile 'com.github.woxthebox:draglistview:1.4.8'
}

Add this to proguard rules, otherwise animations won't work correctly
Expand Down Expand Up @@ -158,25 +158,38 @@ List and Grid layouts are used as example in the sample project.
mBoardView.setSnapToColumnInLandscape(false);
mBoardView.setColumnSnapPosition(BoardView.ColumnSnapPosition.CENTER);
mBoardView.setBoardListener(new BoardView.BoardListener() {
@Override
public void onItemDragStarted(int column, int row) {
Toast.makeText(getActivity(), "Start - column: " + column + " row: " + row, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemChangedColumn(int oldColumn, int newColumn) {
TextView itemCount1 = (TextView) mBoardView.getHeaderView(oldColumn).findViewById(R.id.item_count);
itemCount1.setText("" + mBoardView.getAdapter(oldColumn).getItemCount());
TextView itemCount2 = (TextView) mBoardView.getHeaderView(newColumn).findViewById(R.id.item_count);
itemCount2.setText("" + mBoardView.getAdapter(newColumn).getItemCount());
}

@Override
public void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow) {
if (fromColumn != toColumn || fromRow != toRow) {
Toast.makeText(getActivity(), "End - column: " + toColumn + " row: " + toRow, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemDragStarted(int column, int row) {
Toast.makeText(getActivity(), "Start - column: " + column + " row: " + row, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemChangedColumn(int oldColumn, int newColumn) {
TextView itemCount1 = (TextView) mBoardView.getHeaderView(oldColumn).findViewById(R.id.item_count);
itemCount1.setText("" + mBoardView.getAdapter(oldColumn).getItemCount());
TextView itemCount2 = (TextView) mBoardView.getHeaderView(newColumn).findViewById(R.id.item_count);
itemCount2.setText("" + mBoardView.getAdapter(newColumn).getItemCount());
}

@Override
public void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow) {
if (fromColumn != toColumn || fromRow != toRow) {
Toast.makeText(getActivity(), "End - column: " + toColumn + " row: " + toRow, Toast.LENGTH_SHORT).show();
}
}
});
mBoardView.setBoardCallback(new BoardView.BoardCallback() {
@Override
public boolean canDragItemAtPosition(int column, int dragPosition) {
// Add logic here to prevent an item to be dragged
return true;
}

@Override
public boolean canDropItemAtPosition(int oldColumn, int oldRow, int newColumn, int newRow) {
// Add logic here to prevent an item to be dropped
return true;
}
});
...
mBoardView.addColumnList(listAdapter, header, false);
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 11
Expand Down
4 changes: 2 additions & 2 deletions library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=1.4.7
VERSION_CODE=38
VERSION_NAME=1.4.8
VERSION_CODE=39
GROUP=com.github.woxthebox

POM_DESCRIPTION=Drag and drop to re-order items in a list, grid or board.
Expand Down
43 changes: 36 additions & 7 deletions library/src/main/java/com/woxthebox/draglistview/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public interface BoardListener {
void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow);
}

public interface BoardCallback {
boolean canDragItemAtPosition(int column, int row);

boolean canDropItemAtPosition(int oldColumn, int oldRow, int newColumn, int newRow);
}

public enum ColumnSnapPosition {
LEFT, CENTER, RIGHT
}
Expand All @@ -68,6 +74,7 @@ public enum ColumnSnapPosition {
private DragItemRecyclerView mCurrentRecyclerView;
private DragItem mDragItem;
private BoardListener mBoardListener;
private BoardCallback mBoardCallback;
private boolean mSnapToColumnWhenScrolling = true;
private boolean mSnapToColumnWhenDragging = true;
private boolean mSnapToColumnInLandscape = false;
Expand Down Expand Up @@ -265,14 +272,19 @@ private void updateScrollPosition() {
int oldColumn = getColumnOfList(mCurrentRecyclerView);
int newColumn = getColumnOfList(currentList);
long itemId = mCurrentRecyclerView.getDragItemId();
Object item = mCurrentRecyclerView.removeDragItemAndEnd();
if (item != null) {
mCurrentRecyclerView = currentList;
mCurrentRecyclerView.addDragItemAndStart(getListTouchY(mCurrentRecyclerView), item, itemId);
mDragItem.setOffset(((View) mCurrentRecyclerView.getParent()).getLeft(), mCurrentRecyclerView.getTop());

if (mBoardListener != null) {
mBoardListener.onItemChangedColumn(oldColumn, newColumn);
// Check if it is ok to drop the item in the new column first
int newPosition = currentList.getDragPositionForY(getListTouchY(currentList));
if (mBoardListener == null || mBoardCallback.canDropItemAtPosition(mDragStartColumn, mDragStartRow, newColumn, newPosition)) {
Object item = mCurrentRecyclerView.removeDragItemAndEnd();
if (item != null) {
mCurrentRecyclerView = currentList;
mCurrentRecyclerView.addDragItemAndStart(getListTouchY(mCurrentRecyclerView), item, itemId);
mDragItem.setOffset(((View) mCurrentRecyclerView.getParent()).getLeft(), mCurrentRecyclerView.getTop());

if (mBoardListener != null) {
mBoardListener.onItemChangedColumn(oldColumn, newColumn);
}
}
}
}
Expand Down Expand Up @@ -594,6 +606,10 @@ public void setBoardListener(BoardListener listener) {
mBoardListener = listener;
}

public void setBoardCallback(BoardCallback callback) {
mBoardCallback = callback;
}

public void setCustomDragItem(DragItem dragItem) {
DragItem newDragItem;
if (dragItem != null) {
Expand Down Expand Up @@ -651,6 +667,19 @@ public void onDragEnded(int newItemPosition) {
}
}
});
recyclerView.setDragItemCallback(new DragItemRecyclerView.DragItemCallback() {
@Override
public boolean canDragItemAtPosition(int dragPosition) {
int column = getColumnOfList(recyclerView);
return mBoardCallback == null || mBoardCallback.canDragItemAtPosition(column, dragPosition);
}

@Override
public boolean canDropItemAtPosition(int dropPosition) {
int column = getColumnOfList(recyclerView);
return mBoardCallback == null || mBoardCallback.canDropItemAtPosition(mDragStartColumn, mDragStartRow, column, dropPosition);
}
});

recyclerView.setAdapter(adapter);
recyclerView.setDragEnabled(mDragEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private void onDragItemAnimationEnd() {
invalidate();
}

void addDragItemAndStart(float y, Object item, long itemId) {
int getDragPositionForY(float y) {
View child = findChildView(0, y);
int pos;
if (child == null && getChildCount() > 0) {
Expand All @@ -476,6 +476,11 @@ void addDragItemAndStart(float y, Object item, long itemId) {
if (pos == NO_POSITION) {
pos = 0;
}
return pos;
}

void addDragItemAndStart(float y, Object item, long itemId) {
int pos = getDragPositionForY(y);

mDragState = DragState.DRAG_STARTED;
mDragItemId = itemId;
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
applicationId "com.woxthebox.draglistview.sample"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void onItemDragStarted(int column, int row) {

@Override
public void onItemChangedPosition(int oldColumn, int oldRow, int newColumn, int newRow) {
Toast.makeText(mBoardView.getContext(), "Position changed - column: " + newColumn + " row: " + newRow, Toast.LENGTH_SHORT).show();
//Toast.makeText(mBoardView.getContext(), "Position changed - column: " + newColumn + " row: " + newRow, Toast.LENGTH_SHORT).show();
}

@Override
Expand All @@ -93,6 +93,19 @@ public void onItemDragEnded(int fromColumn, int fromRow, int toColumn, int toRow
}
}
});
mBoardView.setBoardCallback(new BoardView.BoardCallback() {
@Override
public boolean canDragItemAtPosition(int column, int dragPosition) {
// Add logic here to prevent an item to be dragged
return true;
}

@Override
public boolean canDropItemAtPosition(int oldColumn, int oldRow, int newColumn, int newRow) {
// Add logic here to prevent an item to be dropped
return true;
}
});
return view;
}

Expand Down

0 comments on commit cb2bc9a

Please sign in to comment.