Android: update flowlayout beforeDragging & add itemCanDrag

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

View File

@ -30,6 +30,7 @@ import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
@ -119,6 +120,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
private final DoricJSDispatcher jsDispatcher = new DoricJSDispatcher(); private final DoricJSDispatcher jsDispatcher = new DoricJSDispatcher();
private int itemCount = 0; private int itemCount = 0;
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() {
@ -127,15 +129,46 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
} }
@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 = flowAdapter.itemValues.valueAt(srcPosition); String srcValue = flowAdapter.itemValues.valueAt(srcPosition);
String targetValue = flowAdapter.itemValues.valueAt(targetPosition); String targetValue = flowAdapter.itemValues.valueAt(targetPosition);
flowAdapter.itemValues.setValueAt(srcPosition, targetValue); flowAdapter.itemValues.setValueAt(srcPosition, targetValue);
@ -156,6 +189,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
} }
); );
private String itemCanDragFuncId;
private String beforeDraggingFuncId; private String beforeDraggingFuncId;
private String onDraggingFuncId; private String onDraggingFuncId;
private String onDraggedFuncId; private String onDraggedFuncId;
@ -275,6 +309,12 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
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;
@ -322,6 +362,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
if (jsObject.propertySet().size() > 1 || !jsObject.propertySet().contains("subviews")) { if (jsObject.propertySet().size() > 1 || !jsObject.propertySet().contains("subviews")) {
if (mView != null) { if (mView != null) {
mView.post(new Runnable() { mView.post(new Runnable() {
@SuppressLint("NotifyDataSetChanged")
@Override @Override
public void run() { public void run() {
flowAdapter.loadMore = loadMore; flowAdapter.loadMore = loadMore;
@ -494,6 +535,7 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
onScrollFuncId = null; onScrollFuncId = null;
onScrollEndFuncId = null; onScrollEndFuncId = null;
flowAdapter.renderItemFuncId = null; flowAdapter.renderItemFuncId = null;
itemCanDragFuncId = null;
beforeDraggingFuncId = null; beforeDraggingFuncId = null;
onDraggingFuncId = null; onDraggingFuncId = null;
onDraggedFuncId = null; onDraggedFuncId = null;
@ -557,7 +599,17 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
if (onItemTouchCallbackListener != null) {
if (onItemTouchCallbackListener.itemCanDrag(viewHolder.getAdapterPosition())) {
return makeMovementFlags(dragFlags, swipeFlags);
} else {
return 0;
}
} else {
return makeMovementFlags(dragFlags, swipeFlags);
}
} }
@Override @Override
@ -603,6 +655,8 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
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);