Skip to content

Commit

Permalink
fix(android): move node incorrect in ViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel authored and siguangli committed Sep 11, 2023
1 parent 3a9e211 commit 2cf6dd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -228,7 +229,7 @@ public void moveNode(int rootId, int pid, @NonNull List<Object> list) {
LogUtils.w(TAG, "moveNode: get parent failed!");
return;
}
List<RenderNode> moveNodes = null;
List<Pair<RenderNode, Integer>> moveNodes = null;
List<MoveNodeInfo> infoList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
try {
Expand All @@ -244,7 +245,7 @@ public void moveNode(int rootId, int pid, @NonNull List<Object> list) {
Collections.sort(infoList, new Comparator<MoveNodeInfo>() {
@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++) {
Expand All @@ -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) {
Expand All @@ -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<RenderNode> moveNodes = new ArrayList<>(ids.length);
List<Pair<RenderNode, Integer>> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -114,7 +115,7 @@ public class RenderNode {
@Nullable
protected Object mExtra;
@Nullable
protected List<RenderNode> mMoveNodes;
protected List<Pair<RenderNode, Integer>> mMoveNodes;
@Nullable
protected SparseIntArray mDeletedChildren;
@Nullable
Expand Down Expand Up @@ -550,15 +551,15 @@ public int compare(RenderNode n1, RenderNode n2) {
mChildrenUnattached.clear();
}
if (mMoveNodes != null && !mMoveNodes.isEmpty()) {
Collections.sort(mMoveNodes, new Comparator<RenderNode>() {
Collections.sort(mMoveNodes, new Comparator<Pair<RenderNode, Integer>>() {
@Override
public int compare(RenderNode o1, RenderNode o2) {
return o1.indexFromParent() < o2.indexFromParent() ? -1 : 0;
public int compare(Pair<RenderNode, Integer> o1, Pair<RenderNode, Integer> o2) {
return o1.first.indexFromParent() - o2.first.indexFromParent();
}
});
for (RenderNode moveNode : mMoveNodes) {
mControllerManager.moveView(mRootId, moveNode.getId(), mId,
getChildDrawingOrder(moveNode));
for (Pair<RenderNode, Integer> pair : mMoveNodes) {
mControllerManager.moveView(mRootId, pair.first.getId(), pair.second, mId,
getChildDrawingOrder(pair.first));
}
mMoveNodes.clear();
}
Expand Down Expand Up @@ -630,7 +631,7 @@ public void updateLayout(int x, int y, int w, int h) {
setNodeFlag(FLAG_UPDATE_LAYOUT);
}

public void addMoveNodes(@NonNull List<RenderNode> moveNodes) {
public void addMoveNodes(@NonNull List<Pair<RenderNode, Integer>> moveNodes) {
if (mMoveNodes == null) {
mMoveNodes = new ArrayList<>();
}
Expand Down

0 comments on commit 2cf6dd4

Please sign in to comment.