From 2cf6dd4effc7da84c1d19fc5be74cb278d823a11 Mon Sep 17 00:00:00 2001 From: iPel Date: Mon, 11 Sep 2023 12:43:22 +0800 Subject: [PATCH] fix(android): move node incorrect in `ViewPager` --- .../mtt/hippy/uimanager/ControllerManager.java | 13 ++++++++++--- .../mtt/hippy/uimanager/RenderManager.java | 11 ++++++----- .../com/tencent/renderer/node/RenderNode.java | 17 +++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerManager.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerManager.java index d9dc588bea6..942c73d2b73 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerManager.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerManager.java @@ -371,14 +371,21 @@ public void updateExtra(int rootId, int id, String name, @Nullable Object extra) } } - public void moveView(int rootId, int id, int newPid, int index) { + public void moveView(int rootId, int id, int oldPid, int newPid, int index) { View view = mControllerRegistry.getView(rootId, id); if (view == null) { return; } - ViewParent oldParent = view.getParent(); + View oldParent = mControllerRegistry.getView(rootId, oldPid); if (oldParent instanceof ViewGroup) { - ((ViewGroup) oldParent).removeView(view); + String className = NativeViewTag.getClassName(oldParent); + HippyViewController controller = null; + if (className != null) { + controller = mControllerRegistry.getViewController(className); + } + if (controller != null) { + controller.deleteChild((ViewGroup) oldParent, view); + } } View newParent = mControllerRegistry.getView(rootId, newPid); if (newParent instanceof ViewGroup) { diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/RenderManager.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/RenderManager.java index 9cb8e14ac2e..c82d88444c6 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/RenderManager.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/RenderManager.java @@ -23,6 +23,7 @@ import static com.tencent.renderer.node.RenderNode.FLAG_UPDATE_TOTAL_PROPS; import android.content.Context; +import android.util.Pair; import android.view.View; import androidx.annotation.NonNull; @@ -228,7 +229,7 @@ public void moveNode(int rootId, int pid, @NonNull List list) { LogUtils.w(TAG, "moveNode: get parent failed!"); return; } - List moveNodes = null; + List> moveNodes = null; List infoList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { try { @@ -244,7 +245,7 @@ public void moveNode(int rootId, int pid, @NonNull List list) { Collections.sort(infoList, new Comparator() { @Override public int compare(MoveNodeInfo n1, MoveNodeInfo n2) { - return n1.index < n2.index ? -1 : 0; + return n1.index - n2.index; } }); for (int i = 0; i < infoList.size(); i++) { @@ -263,7 +264,7 @@ public int compare(MoveNodeInfo n1, MoveNodeInfo n2) { if (moveNodes == null) { moveNodes = new ArrayList<>(); } - moveNodes.add(child); + moveNodes.add(new Pair<>(child, pid)); } parent.resetChildIndex(child, info.index); } catch (Exception e) { @@ -283,11 +284,11 @@ public void moveNode(int rootId, int[] ids, int newPid, int oldPid, int insertIn LogUtils.w(TAG, "moveNode: oldParent=" + oldParent + ", newParent=" + newParent); return; } - List moveNodes = new ArrayList<>(ids.length); + List> moveNodes = new ArrayList<>(ids.length); for (int i = 0; i < ids.length; i++) { RenderNode node = getRenderNode(rootId, ids[i]); if (node != null) { - moveNodes.add(node); + moveNodes.add(new Pair<>(node, oldPid)); oldParent.removeChild(node); newParent.addChild(node, (i + insertIndex)); } diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java b/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java index 6ea1ca4665b..a180383428a 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java @@ -19,6 +19,7 @@ import static com.tencent.renderer.NativeRenderException.ExceptionCode.REUSE_VIEW_HAS_ABANDONED_NODE_ERR; import android.text.TextUtils; +import android.util.Pair; import android.util.SparseIntArray; import android.view.View; @@ -114,7 +115,7 @@ public class RenderNode { @Nullable protected Object mExtra; @Nullable - protected List mMoveNodes; + protected List> mMoveNodes; @Nullable protected SparseIntArray mDeletedChildren; @Nullable @@ -550,15 +551,15 @@ public int compare(RenderNode n1, RenderNode n2) { mChildrenUnattached.clear(); } if (mMoveNodes != null && !mMoveNodes.isEmpty()) { - Collections.sort(mMoveNodes, new Comparator() { + Collections.sort(mMoveNodes, new Comparator>() { @Override - public int compare(RenderNode o1, RenderNode o2) { - return o1.indexFromParent() < o2.indexFromParent() ? -1 : 0; + public int compare(Pair o1, Pair o2) { + return o1.first.indexFromParent() - o2.first.indexFromParent(); } }); - for (RenderNode moveNode : mMoveNodes) { - mControllerManager.moveView(mRootId, moveNode.getId(), mId, - getChildDrawingOrder(moveNode)); + for (Pair pair : mMoveNodes) { + mControllerManager.moveView(mRootId, pair.first.getId(), pair.second, mId, + getChildDrawingOrder(pair.first)); } mMoveNodes.clear(); } @@ -630,7 +631,7 @@ public void updateLayout(int x, int y, int w, int h) { setNodeFlag(FLAG_UPDATE_LAYOUT); } - public void addMoveNodes(@NonNull List moveNodes) { + public void addMoveNodes(@NonNull List> moveNodes) { if (mMoveNodes == null) { mMoveNodes = new ArrayList<>(); }