Android: update list beforeDragging & add itemCanDrag

This commit is contained in:
王劲鹏 2022-08-26 14:48:00 +08:00 committed by osborn
parent 6236429e25
commit c9eb2fd8bb

View File

@ -32,6 +32,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNumber; import com.github.pengfeizhou.jscore.JSNumber;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
@ -81,6 +82,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
private boolean scrollable = true; private boolean scrollable = true;
private final Set<Integer> swapDisabled = new HashSet<>();
private final DoricItemTouchHelperCallback doricItemTouchHelperCallback = new DoricItemTouchHelperCallback( private final DoricItemTouchHelperCallback doricItemTouchHelperCallback = new DoricItemTouchHelperCallback(
new OnItemTouchCallbackListener() { new OnItemTouchCallbackListener() {
@ -89,15 +91,46 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
@Override
public Boolean itemCanDrag(int fromPos) {
AsyncResult<JSDecoder> asyncResult = callJSResponse(itemCanDragFuncId, fromPos);
JSDecoder jsDecoder = asyncResult.synchronous().get();
try {
JSValue jsValue = jsDecoder.decode();
if (jsValue.isBoolean()) {
return jsValue.asBoolean().value();
}
} catch (ArchiveException e) {
e.printStackTrace();
}
return true;
}
@Override @Override
public void beforeMove(int fromPos) { public void beforeMove(int fromPos) {
swapDisabled.clear();
if (!TextUtils.isEmpty(beforeDraggingFuncId)) { if (!TextUtils.isEmpty(beforeDraggingFuncId)) {
callJSResponse(beforeDraggingFuncId, fromPos); AsyncResult<JSDecoder> asyncResult = callJSResponse(beforeDraggingFuncId, fromPos);
JSDecoder jsDecoder = asyncResult.synchronous().get();
try {
JSValue jsValue = jsDecoder.decode();
if (jsValue.isArray()) {
int[] intArray = jsValue.asArray().toIntArray();
for (int i = 0; i != intArray.length; i++) {
swapDisabled.add(intArray[i]);
}
}
} catch (ArchiveException e) {
e.printStackTrace();
}
} }
} }
@Override @Override
public boolean onMove(int srcPosition, int targetPosition) { public boolean onMove(int srcPosition, int targetPosition) {
if (swapDisabled.contains(targetPosition)) {
return false;
}
String srcValue = itemValues.valueAt(srcPosition); String srcValue = itemValues.valueAt(srcPosition);
String targetValue = itemValues.valueAt(targetPosition); String targetValue = itemValues.valueAt(targetPosition);
itemValues.setValueAt(srcPosition, targetValue); itemValues.setValueAt(srcPosition, targetValue);
@ -118,6 +151,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
); );
private String itemCanDragFuncId;
private String beforeDraggingFuncId; private String beforeDraggingFuncId;
private String onDraggingFuncId; private String onDraggingFuncId;
private String onDraggedFuncId; private String onDraggedFuncId;
@ -319,6 +353,12 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
boolean canDrag = prop.asBoolean().value(); boolean canDrag = prop.asBoolean().value();
doricItemTouchHelperCallback.setDragEnable(canDrag); doricItemTouchHelperCallback.setDragEnable(canDrag);
break; break;
case "itemCanDrag":
if (!prop.isString()) {
return;
}
this.itemCanDragFuncId = prop.asString().value();
break;
case "beforeDragging": case "beforeDragging":
if (!prop.isString()) { if (!prop.isString()) {
return; return;
@ -468,6 +508,7 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
onScrollFuncId = null; onScrollFuncId = null;
onScrollEndFuncId = null; onScrollEndFuncId = null;
renderItemFuncId = null; renderItemFuncId = null;
itemCanDragFuncId = null;
beforeDraggingFuncId = null; beforeDraggingFuncId = null;
onDraggingFuncId = null; onDraggingFuncId = null;
onDraggedFuncId = null; onDraggedFuncId = null;
@ -538,7 +579,16 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN; dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
} }
if (onItemTouchCallbackListener != null) {
if (onItemTouchCallbackListener.itemCanDrag(viewHolder.getAdapterPosition())) {
return makeMovementFlags(dragFlag, swipeFlag); return makeMovementFlags(dragFlag, swipeFlag);
} else {
return 0;
}
} else {
return makeMovementFlags(dragFlag, swipeFlag);
}
} }
return 0; return 0;
} }
@ -586,6 +636,8 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
private interface OnItemTouchCallbackListener { private interface OnItemTouchCallbackListener {
void onSwiped(int adapterPosition); void onSwiped(int adapterPosition);
Boolean itemCanDrag(int fromPos);
void beforeMove(int fromPos); void beforeMove(int fromPos);
boolean onMove(int srcPosition, int targetPosition); boolean onMove(int srcPosition, int targetPosition);