From 481e643ff972d7c6269532c5833520c68ebbdbe7 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 27 Oct 2021 17:29:33 +0800 Subject: [PATCH] android: reset view node when reuse this node --- .../java/pub/doric/shader/DoricLayer.java | 5 +++ .../main/java/pub/doric/shader/ImageNode.java | 16 +++++++++ .../main/java/pub/doric/shader/InputNode.java | 13 ++++++++ .../java/pub/doric/shader/SwitchNode.java | 7 ++++ .../main/java/pub/doric/shader/TextNode.java | 14 ++++++++ .../main/java/pub/doric/shader/ViewNode.java | 33 +++++++++++++++++++ .../shader/flowlayout/FlowLayoutNode.java | 12 +++++++ .../java/pub/doric/shader/list/ListNode.java | 12 +++++++ .../doric/shader/slider/NestedSliderNode.java | 7 ++++ .../pub/doric/shader/slider/SliderNode.java | 8 +++++ doric-iOS/Pod/Classes/Shader/DoricImageNode.m | 3 ++ doric-iOS/Pod/Classes/Shader/DoricViewNode.m | 4 +++ 12 files changed, 134 insertions(+) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/DoricLayer.java b/doric-android/doric/src/main/java/pub/doric/shader/DoricLayer.java index 7ee1e5ad..5187e70c 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/DoricLayer.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/DoricLayer.java @@ -177,4 +177,9 @@ public class DoricLayer extends MaximumFrameLayout { return 0; } + public void reset() { + mCornerRadii = null; + mBorderPaint = null; + mShadowPaint = null; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java index a52fbba7..6fb54456 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java @@ -499,4 +499,20 @@ public class ImageNode extends ViewNode { ((Animatable) drawable).stop(); } } + + @Override + protected void reset() { + super.reset(); + mView.setImageDrawable(null); + mView.setScaleType(ImageView.ScaleType.CENTER_CROP); + loadCallbackId = ""; + isBlur = false; + placeHolderImage = null; + placeHolderColor = Color.TRANSPARENT; + placeHolderImageBase64 = null; + errorImage = null; + errorColor = Color.TRANSPARENT; + errorImageBase64 = null; + imageScale = DoricUtils.getScreenScale(); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/InputNode.java b/doric-android/doric/src/main/java/pub/doric/shader/InputNode.java index 43d0a47b..ab351670 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/InputNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/InputNode.java @@ -16,6 +16,7 @@ package pub.doric.shader; import android.content.Context; +import android.graphics.Color; import android.graphics.Typeface; import android.text.Editable; import android.text.InputFilter; @@ -478,4 +479,16 @@ public class InputNode extends ViewNode implements TextWatcher, View.O } return ret ? null : ""; } + + @Override + protected void reset() { + super.reset(); + mView.setText(""); + mView.setTextColor(Color.BLACK); + mView.setHint(""); + mView.setHintTextColor(Color.GRAY); + onTextChangeId = null; + onFocusChangeId = null; + beforeTextChangeId = null; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/SwitchNode.java b/doric-android/doric/src/main/java/pub/doric/shader/SwitchNode.java index 38b9bbb9..a3a838b6 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/SwitchNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/SwitchNode.java @@ -115,4 +115,11 @@ public class SwitchNode extends ViewNode { mView.setThumbTintList(thumbTintList); mView.setTrackTintList(trackTintList); } + + @Override + protected void reset() { + super.reset(); + mView.setChecked(false); + mView.setOnCheckedChangeListener(null); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index 756c93b7..f1dd4b82 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -409,4 +409,18 @@ public class TextNode extends ViewNode { textView.getPaint().setShader(textShader); textView.invalidate(); } + + @Override + protected void reset() { + super.reset(); + mView.setText(""); + mView.setTextColor(Color.BLACK); + mView.setGravity(Gravity.CENTER); + mView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); + mView.setMaxLines(1); + mView.setSingleLine(true); + mView.setEllipsize(TextUtils.TruncateAt.END); + mView.getPaint().setStrikeThruText(false); + mView.getPaint().setUnderlineText(false); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ViewNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ViewNode.java index 2e56fa4d..4e00d748 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ViewNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ViewNode.java @@ -144,6 +144,9 @@ public abstract class ViewNode extends DoricContextHolder { protected abstract T build(); public void blend(JSObject jsObject) { + if (mSuperNode != null && mSuperNode.mReusable) { + reset(); + } if (jsObject != null) { JSValue value = jsObject.getProperty("layoutConfig"); if (value.isObject()) { @@ -1186,4 +1189,34 @@ public abstract class ViewNode extends DoricContextHolder { .put("y", DoricUtils.px2dp(position[1])) .toJSONObject(); } + + protected void reset() { + ViewGroup.LayoutParams layoutParams = getLayoutParams(); + layoutParams.width = 0; + layoutParams.height = 0; + if (layoutParams instanceof ViewGroup.MarginLayoutParams) { + ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = 0; + ((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = 0; + ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = 0; + ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = 0; + } + setBackgroundColor(Color.TRANSPARENT); + setAlpha(1); + setTranslationX(0); + setTranslationY(0); + setScaleX(0); + setScaleY(0); + setRotation(0); + setRotationX(0); + setRotationY(0); + mView.setPadding(0, 0, 0, 0); + mView.setOnClickListener(null); + DoricLayer doricLayer = this.doricLayer; + if (mView instanceof DoricLayer) { + doricLayer = (DoricLayer) mView; + } + if (doricLayer != null) { + doricLayer.reset(); + } + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java b/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java index 953a07b4..d2551742 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java @@ -383,4 +383,16 @@ public class FlowLayoutNode extends SuperNode implements IDoricScr } return jsonArray; } + + @Override + protected void reset() { + super.reset(); + scrollable = true; + loadMore = false; + loadMoreViewId = null; + onLoadMoreFuncId = null; + onScrollFuncId = null; + onScrollEndFuncId = null; + flowAdapter.renderItemFuncId = null; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java b/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java index 33c3bb99..34c18e7b 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/list/ListNode.java @@ -352,4 +352,16 @@ public class ListNode extends SuperNode implements IDoricScrollabl mView.scrollToPosition(pos); } } + + @Override + protected void reset() { + super.reset(); + scrollable = true; + loadMore = false; + loadMoreViewId = null; + onLoadMoreFuncId = null; + onScrollFuncId = null; + onScrollEndFuncId = null; + renderItemFuncId = null; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/slider/NestedSliderNode.java b/doric-android/doric/src/main/java/pub/doric/shader/slider/NestedSliderNode.java index 8ceae5d5..8dcea373 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/slider/NestedSliderNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/slider/NestedSliderNode.java @@ -252,4 +252,11 @@ public class NestedSliderNode extends GroupNode implements ViewPager. public int getSlidedPage() { return mView.getCurrentItem(); } + + @Override + protected void reset() { + super.reset(); + onPageSlidedFuncId = null; + ((CustomViewPager) mView).setScrollable(true); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java index b3b42c6d..2bbb778d 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java @@ -248,4 +248,12 @@ public class SliderNode extends SuperNode { return pageIndex; } } + + @Override + protected void reset() { + super.reset(); + scrollable = true; + onPageSlidedFuncId = null; + renderPageFuncId = null; + } } diff --git a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m index a0523d02..cd21064f 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m @@ -636,5 +636,8 @@ - (void)reset { self.errorColor = nil; self.errorImage = nil; self.errorImageBase64 = nil; + self.imageScale = UIScreen.mainScreen.scale; + self.blurEffectView = nil; + self.view.contentMode = UIViewContentModeScaleAspectFill; } @end diff --git a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m index de96753c..02ef3e59 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m @@ -933,7 +933,11 @@ - (CAMediaTimingFunction *)translateToTimingFunction:(NSNumber *)timingFunction - (void)reset { self.view.backgroundColor = UIColor.clearColor; + DoricLayoutType doricLayoutType = self.view.doricLayout.layoutType; self.view.doricLayout = [[DoricLayout new] also:^(DoricLayout *it) { + it.width = 0; + it.height = 0; + it.layoutType = doricLayoutType; it.view = self.view; }];