From 16cbcc53ebfd7d73966cbb8317f7ae62c530fc8c Mon Sep 17 00:00:00 2001 From: longerian Date: Fri, 2 Mar 2018 11:35:58 +0800 Subject: [PATCH 1/7] opt layout params instance in OnePlusNLayoutHelper --- .../vlayout/layout/BaseLayoutHelper.java | 6 +- .../vlayout/layout/OnePlusNLayoutHelper.java | 95 ++++++++---------- .../layout/OnePlusNLayoutHelperEx.java | 98 ++++++++----------- 3 files changed, 83 insertions(+), 116 deletions(-) diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/BaseLayoutHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/BaseLayoutHelper.java index b67040b0..13980db8 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/BaseLayoutHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/BaseLayoutHelper.java @@ -507,14 +507,14 @@ protected void handleStateOnResult(LayoutChunkResult result, View view) { /** * Helper methods to handle focus states for views - * FIXME 可变参数性能不好,会引起一次潜在的数组对象创建,在频繁滑动过程中,容易引起GC,如果只有一个View,建议调用上述方法 * @param result * @param views */ - protected void handleStateOnResult(LayoutChunkResult result, View... views) { + protected void handleStateOnResult(LayoutChunkResult result, View[] views) { if (views == null) return; - for (View view : views) { + for (int i = 0; i < views.length; i++) { + View view = views[i]; if (view == null) { continue; } diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelper.java index fabfcd99..f6943687 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelper.java @@ -30,6 +30,7 @@ import com.alibaba.android.vlayout.OrientationHelperEx; import com.alibaba.android.vlayout.VirtualLayoutManager; import com.alibaba.android.vlayout.VirtualLayoutManager.AnchorInfoWrapper; +import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams; import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutStateWrapper; import android.graphics.Rect; @@ -268,7 +269,7 @@ public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state } } - private float getViewMainWeight(ViewGroup.MarginLayoutParams params, int index) { + private float getViewMainWeight(int index) { if (mColWeights.length > index) { return mColWeights[index]; } @@ -317,8 +318,8 @@ private int handleHeader(View header, LayoutStateWrapper layoutState, LayoutChun } OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); - final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( - header.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp = (LayoutParams) header.getLayoutParams(); + // fill width int widthSpec = helper.getChildMeasureSpec(parentWidth - parentHPadding, @@ -337,8 +338,7 @@ private int handleFooter(View footer, LayoutStateWrapper layoutState, LayoutChun OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); - final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( - footer.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp = (LayoutParams) footer.getLayoutParams(); // fill width int widthSpec = helper.getChildMeasureSpec(parentWidth - parentHPadding, @@ -355,8 +355,7 @@ private int handleOne(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); View view = mChildrenViews[0]; - final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams) view.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp = (LayoutParams) view.getLayoutParams(); if (!Float.isNaN(mAspectRatio)) { if (layoutInVertical) { @@ -366,7 +365,7 @@ private int handleOne(LayoutStateWrapper layoutState, LayoutChunkResult result, } } - final float weight = getViewMainWeight(lp, 0); + final float weight = getViewMainWeight(0); // fill width int widthSpec = helper.getChildMeasureSpec( @@ -384,7 +383,7 @@ private int handleOne(LayoutStateWrapper layoutState, LayoutChunkResult result, layoutChildWithMargin(view, mAreaRect.left, mAreaRect.top, mAreaRect.right, mAreaRect.bottom, helper); - handleStateOnResult(result, null, view, null); + handleStateOnResult(result, view); mainConsumed = mAreaRect.bottom - mAreaRect.top + (hasHeader ? 0 : mMarginTop + mPaddingTop) + (hasFooter ? 0 : mMarginBottom + mPaddingBottom); return mainConsumed; } @@ -395,13 +394,11 @@ private int handleTwo(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final ViewGroup.MarginLayoutParams lp1 = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = mChildrenViews[1]; - final ViewGroup.MarginLayoutParams lp2 = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams)child2.getLayoutParams()); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); if (layoutInVertical) { @@ -486,7 +483,7 @@ private int handleTwo(LayoutStateWrapper layoutState, LayoutChunkResult result, mainConsumed = mAreaRect.right - mAreaRect.left + (hasHeader ? 0 : mMarginLeft + mPaddingRight) + (hasFooter ? 0 : mMarginRight + mPaddingRight); } - handleStateOnResult(result, null, child1, child2, null); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } @@ -496,19 +493,16 @@ private int handleThree(LayoutStateWrapper layoutState, LayoutChunkResult result OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final ViewGroup.MarginLayoutParams lp1 = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[1]; final View child3 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[2]; - final ViewGroup.MarginLayoutParams lp2 = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams)child2.getLayoutParams()); - final ViewGroup.MarginLayoutParams lp3 = new ViewGroup.MarginLayoutParams( - (ViewGroup.MarginLayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); if (layoutInVertical) { @@ -579,7 +573,7 @@ private int handleThree(LayoutStateWrapper layoutState, LayoutChunkResult result // TODO: horizontal support } - handleStateOnResult(result, null, child1, child2, child3, null); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } @@ -590,22 +584,18 @@ private int handleFour(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[1]; - final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child2.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); final View child3 = mChildrenViews[2]; - final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); final View child4 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[3]; - final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child4.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp4 = (VirtualLayoutManager.LayoutParams) child4.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); - final float weight4 = getViewMainWeight(lp1, 3); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); + final float weight4 = getViewMainWeight(3); if (layoutInVertical) { @@ -691,7 +681,7 @@ private int handleFour(LayoutStateWrapper layoutState, LayoutChunkResult result, // TODO: horizontal support } - handleStateOnResult(result, null, child1, child2, child3, child4, null); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } @@ -701,26 +691,21 @@ private int handleFive(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[1]; - final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child2.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); final View child3 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[2]; - final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); final View child4 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[3]; - final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child4.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp4 = (VirtualLayoutManager.LayoutParams) child4.getLayoutParams(); final View child5 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[4]; - final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child5.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp5 = (VirtualLayoutManager.LayoutParams) child5.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); - final float weight4 = getViewMainWeight(lp1, 3); - final float weight5 = getViewMainWeight(lp1, 4); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); + final float weight4 = getViewMainWeight(3); + final float weight5 = getViewMainWeight(4); if (layoutInVertical) { @@ -819,7 +804,7 @@ private int handleFive(LayoutStateWrapper layoutState, LayoutChunkResult result, // TODO: horizontal support } - handleStateOnResult(result, null, child1, child2, child3, child4, child5, null); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelperEx.java b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelperEx.java index 07526ced..03eea1a1 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelperEx.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/OnePlusNLayoutHelperEx.java @@ -185,7 +185,7 @@ public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state } - private float getViewMainWeight(ViewGroup.MarginLayoutParams params, int index) { + private float getViewMainWeight(int index) { if (mColWeights.length > index) { return mColWeights[index]; } @@ -228,26 +228,21 @@ private int handleFive(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[1]; - final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child2.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); final View child3 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[2]; - final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); final View child4 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[3]; - final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child4.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp4 = (VirtualLayoutManager.LayoutParams) child4.getLayoutParams(); final View child5 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[4]; - final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child5.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp5 = (VirtualLayoutManager.LayoutParams) child5.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); - final float weight4 = getViewMainWeight(lp1, 3); - final float weight5 = getViewMainWeight(lp1, 4); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); + final float weight4 = getViewMainWeight(3); + final float weight5 = getViewMainWeight(4); if (layoutInVertical) { @@ -349,7 +344,7 @@ private int handleFive(LayoutStateWrapper layoutState, LayoutChunkResult result, // TODO: horizontal support } - handleStateOnResult(result, child1, child2, child3, child4, child5); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } @@ -361,30 +356,24 @@ private int handSix(LayoutStateWrapper layoutState, LayoutChunkResult result, La OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[5] : mChildrenViews[1]; - final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child2.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); final View child3 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[2]; - final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); final View child4 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[3]; - final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child4.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp4 = (VirtualLayoutManager.LayoutParams) child4.getLayoutParams(); final View child5 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[4]; - final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child5.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp5 = (VirtualLayoutManager.LayoutParams) child5.getLayoutParams(); final View child6 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[5]; - final VirtualLayoutManager.LayoutParams lp6 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child6.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp6 = (VirtualLayoutManager.LayoutParams) child6.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); - final float weight4 = getViewMainWeight(lp1, 3); - final float weight5 = getViewMainWeight(lp1, 4); - final float weight6 = getViewMainWeight(lp1, 5); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); + final float weight4 = getViewMainWeight(3); + final float weight5 = getViewMainWeight(4); + final float weight6 = getViewMainWeight(5); if (layoutInVertical) { @@ -512,7 +501,7 @@ private int handSix(LayoutStateWrapper layoutState, LayoutChunkResult result, La // TODO: horizontal support } - handleStateOnResult(result, child1, child2, child3, child4, child5, child6); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } @@ -522,34 +511,27 @@ private int handSeven(LayoutStateWrapper layoutState, LayoutChunkResult result, OrientationHelperEx orientationHelper = helper.getMainOrientationHelper(); final View child1 = mChildrenViews[0]; - final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child1.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp1 = (VirtualLayoutManager.LayoutParams) child1.getLayoutParams(); final View child2 = helper.getReverseLayout() ? mChildrenViews[6] : mChildrenViews[1]; - final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child2.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp2 = (VirtualLayoutManager.LayoutParams) child2.getLayoutParams(); final View child3 = helper.getReverseLayout() ? mChildrenViews[5] : mChildrenViews[2]; - final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child3.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp3 = (VirtualLayoutManager.LayoutParams) child3.getLayoutParams(); final View child4 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[3]; - final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child4.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp4 = (VirtualLayoutManager.LayoutParams) child4.getLayoutParams(); final View child5 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[4]; - final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child5.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp5 = (VirtualLayoutManager.LayoutParams) child5.getLayoutParams(); final View child6 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[5]; - final VirtualLayoutManager.LayoutParams lp6 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child6.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp6 = (VirtualLayoutManager.LayoutParams) child6.getLayoutParams(); final View child7 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[6]; - final VirtualLayoutManager.LayoutParams lp7 = new VirtualLayoutManager.LayoutParams( - (VirtualLayoutManager.LayoutParams)child7.getLayoutParams()); + final VirtualLayoutManager.LayoutParams lp7 = (VirtualLayoutManager.LayoutParams) child7.getLayoutParams(); - final float weight1 = getViewMainWeight(lp1, 0); - final float weight2 = getViewMainWeight(lp1, 1); - final float weight3 = getViewMainWeight(lp1, 2); - final float weight4 = getViewMainWeight(lp1, 3); - final float weight5 = getViewMainWeight(lp1, 4); - final float weight6 = getViewMainWeight(lp1, 5); - final float weight7 = getViewMainWeight(lp1, 6); + final float weight1 = getViewMainWeight(0); + final float weight2 = getViewMainWeight(1); + final float weight3 = getViewMainWeight(2); + final float weight4 = getViewMainWeight(3); + final float weight5 = getViewMainWeight(4); + final float weight6 = getViewMainWeight(5); + final float weight7 = getViewMainWeight(6); if (layoutInVertical) { @@ -677,7 +659,7 @@ private int handSeven(LayoutStateWrapper layoutState, LayoutChunkResult result, // TODO: horizontal support } - handleStateOnResult(result, child1, child2, child3, child4, child5, child6); + handleStateOnResult(result, mChildrenViews); return mainConsumed; } } From aa09e9e1ef899d8cb37d64aab75a1ac23a6f1458 Mon Sep 17 00:00:00 2001 From: longerian Date: Fri, 2 Mar 2018 14:45:18 +0800 Subject: [PATCH 2/7] use for-i instead of for-each to reduce memory alloc --- .../android/vlayout/LayoutHelperFinder.java | 6 +- .../vlayout/RangeLayoutHelperFinder.java | 47 +++++---------- .../android/vlayout/VirtualLayoutManager.java | 60 +++++++++++++------ .../layout/StaggeredGridLayoutHelper.java | 30 ++++++---- 4 files changed, 80 insertions(+), 63 deletions(-) diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/LayoutHelperFinder.java b/vlayout/src/main/java/com/alibaba/android/vlayout/LayoutHelperFinder.java index 76ddd28c..09f876df 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/LayoutHelperFinder.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/LayoutHelperFinder.java @@ -32,7 +32,7 @@ /** * LayoutHelperFinder provides as repository of LayoutHelpers */ -public abstract class LayoutHelperFinder implements Iterable { +public abstract class LayoutHelperFinder { /** * Put layouts into the finder @@ -57,10 +57,10 @@ public abstract class LayoutHelperFinder implements Iterable { protected abstract List getLayoutHelpers(); /** - * Get iterator that in reverse order + * Get layoutHelpers that in reverse order * * @return */ - protected abstract Iterable reverse(); + protected abstract List reverse(); } diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java b/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java index a151d4d5..cde8451e 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java @@ -29,13 +29,11 @@ import java.util.Collections; import java.util.Comparator; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; /** - An implement of {@link LayoutHelperFinder} which finds layoutHelpers by position + * An implement of {@link LayoutHelperFinder} which finds layoutHelpers by position */ public class RangeLayoutHelperFinder extends LayoutHelperFinder { @@ -45,6 +43,9 @@ public class RangeLayoutHelperFinder extends LayoutHelperFinder { @NonNull private List mLayoutHelpers = new LinkedList<>(); + @NonNull + private List mReverseLayoutHelpers =new LinkedList<>(); + @NonNull private Comparator mLayoutHelperItemComparator = new Comparator() { @Override @@ -54,31 +55,8 @@ public int compare(LayoutHelperItem lhs, LayoutHelperItem rhs) { }; @Override - public Iterator iterator() { - return Collections.unmodifiableList(mLayoutHelpers).iterator(); - } - - @Override - protected Iterable reverse() { - final ListIterator i = mLayoutHelpers.listIterator(mLayoutHelpers.size()); - return new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - public boolean hasNext() { - return i.hasPrevious(); - } - - public LayoutHelper next() { - return i.previous(); - } - - public void remove() { - i.remove(); - } - }; - } - }; + protected List reverse() { + return mReverseLayoutHelpers; } /** @@ -87,12 +65,17 @@ public void remove() { @Override public void setLayouts(@Nullable List layouts) { mLayoutHelpers.clear(); + mReverseLayoutHelpers.clear(); mLayoutHelperItems.clear(); if (layouts != null) { - for (LayoutHelper helper : layouts) { + for (int i = 0, size = layouts.size(); i < size; i++) { + LayoutHelper helper = layouts.get(i); mLayoutHelpers.add(helper); mLayoutHelperItems.add(new LayoutHelperItem(helper)); } + for (int i = layouts.size() - 1; i >= 0; i--) { + mReverseLayoutHelpers.add(layouts.get(i)); + } Collections.sort(mLayoutHelperItems, mLayoutHelperItemComparator); } @@ -101,7 +84,7 @@ public void setLayouts(@Nullable List layouts) { @NonNull @Override protected List getLayoutHelpers() { - return Collections.unmodifiableList(mLayoutHelpers); + return mLayoutHelpers; } @Nullable @@ -123,8 +106,9 @@ public LayoutHelper getLayoutHelper(int position) { e = m - 1; } else if (rs.getEndPosition() < position) { s = m + 1; - } else if (rs.getStartPosition() <= position && rs.getEndPosition() >= position) + } else if (rs.getStartPosition() <= position && rs.getEndPosition() >= position) { break; + } rs = null; } @@ -132,7 +116,6 @@ public LayoutHelper getLayoutHelper(int position) { return rs == null ? null : rs.layoutHelper; } - static class LayoutHelperItem { LayoutHelperItem(LayoutHelper helper) { diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java index 5fb17e5c..84deba6d 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java @@ -149,7 +149,9 @@ public void setHelperFinder(@NonNull final LayoutHelperFinder finder) { List helpers = new LinkedList<>(); if (this.mHelperFinder != null) { - for (LayoutHelper helper : mHelperFinder) { + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper helper = layoutHelpers.get(i); helpers.add(helper); } } @@ -183,7 +185,9 @@ public void setFixOffset(int left, int top, int right, int bottom) { * @param helpers group of layoutHelpers */ public void setLayoutHelpers(@Nullable List helpers) { - for (LayoutHelper helper : mHelperFinder) { + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper helper = layoutHelpers.get(i); oldHelpersSet.put(System.identityHashCode(helper), helper); } @@ -214,7 +218,9 @@ public void setLayoutHelpers(@Nullable List helpers) { this.mHelperFinder.setLayouts(helpers); - for (LayoutHelper helper : mHelperFinder) { + layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper helper = layoutHelpers.get(i); newHelpersSet.put(System.identityHashCode(helper), helper); } @@ -332,7 +338,9 @@ public void onAnchorReady(RecyclerView.State state, ExposeLinearLayoutManagerEx. mTempAnchorInfoWrapper.position = anchorInfo.mPosition; mTempAnchorInfoWrapper.coordinate = anchorInfo.mCoordinate; - for (LayoutHelper layoutHelper : mHelperFinder) { + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); layoutHelper.onRefreshLayout(state, mTempAnchorInfoWrapper, this); } } @@ -400,7 +408,9 @@ public int obtainExtraMargin(View child, boolean isLayoutEnd, boolean useAnchor) private void runPreLayout(RecyclerView.Recycler recycler, RecyclerView.State state) { if (mNested == 0) { - for (LayoutHelper layoutHelper : mHelperFinder.reverse()) { + List reverseLayoutHelpers = mHelperFinder.reverse(); + for (int i = 0, size = reverseLayoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = reverseLayoutHelpers.get(i); layoutHelper.beforeLayout(recycler, state, this); } } @@ -414,7 +424,9 @@ private void runPostLayout(RecyclerView.Recycler recycler, RecyclerView.State st mNested = 0; final int startPosition = findFirstVisibleItemPosition(); final int endPosition = findLastVisibleItemPosition(); - for (LayoutHelper layoutHelper : mHelperFinder) { + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); try { layoutHelper.afterLayout(recycler, state, startPosition, endPosition, scrolled, this); } catch (Exception e) { @@ -566,8 +578,10 @@ public void onScrollStateChanged(int state) { int startPosition = findFirstVisibleItemPosition(); int endPosition = findLastVisibleItemPosition(); - for (LayoutHelper helper : mHelperFinder) { - helper.onScrollStateChanged(state, startPosition, endPosition, this); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + layoutHelper.onScrollStateChanged(state, startPosition, endPosition, this); } } @@ -575,16 +589,20 @@ public void onScrollStateChanged(int state) { public void offsetChildrenHorizontal(int dx) { super.offsetChildrenHorizontal(dx); - for (LayoutHelper helper : mHelperFinder) { - helper.onOffsetChildrenHorizontal(dx, this); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + layoutHelper.onOffsetChildrenHorizontal(dx, this); } } @Override public void offsetChildrenVertical(int dy) { super.offsetChildrenVertical(dy); - for (LayoutHelper helper : mHelperFinder) { - helper.onOffsetChildrenVertical(dy, this); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + layoutHelper.onOffsetChildrenVertical(dy, this); } } @@ -784,8 +802,10 @@ public void onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCo @Override public void onItemsChanged(RecyclerView recyclerView) { - for (LayoutHelper helper : mHelperFinder) { - helper.onItemsChanged(this); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + layoutHelper.onItemsChanged(this); } // setLayoutHelpers(mHelperFinder.getLayoutHelpers()); @@ -837,8 +857,10 @@ public void onAttachedToWindow(RecyclerView view) { public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) { super.onDetachedFromWindow(view, recycler); - for (LayoutHelper helper : mHelperFinder) { - helper.clear(this); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + layoutHelper.clear(this); } mRecyclerView = null; @@ -1069,8 +1091,10 @@ public List getFixedViews() { // TODO: support zIndex? List views = new LinkedList<>(); - for (LayoutHelper helper : mHelperFinder) { - View fixedView = helper.getFixedView(); + List layoutHelpers = mHelperFinder.getLayoutHelpers(); + for (int i = 0, size = layoutHelpers.size(); i < size; i++) { + LayoutHelper layoutHelper = layoutHelpers.get(i); + View fixedView = layoutHelper.getFixedView(); if (fixedView != null) { views.add(fixedView); } diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StaggeredGridLayoutHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StaggeredGridLayoutHelper.java index 358b5dbe..d1df4a88 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StaggeredGridLayoutHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StaggeredGridLayoutHelper.java @@ -359,13 +359,15 @@ public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state // reach the end of layout, cache the gap // TODO: how to retain gap if (layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START) { - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; if (span.mCachedStart != INVALID_LINE) { span.mLastEdgeStart = span.mCachedStart; } } } else { - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; if (span.mCachedEnd != INVALID_LINE) { span.mLastEdgeEnd = span.mCachedEnd; } @@ -594,7 +596,8 @@ private void checkForGaps() { //FIXME do not clear loopup, may cause lane error while scroll //mLazySpanLookup.clear(); - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.setLine(alignLine); } @@ -619,7 +622,8 @@ private View hasGapsToFix(VirtualLayoutManager layoutManager, final int position BitSet mSpansToCheck = new BitSet(mNumLanes); mSpansToCheck.set(0, mNumLanes, true); - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; if (span.mViews.size() != 0 && checkSpanForGap(span, layoutManager, alignLine)) { return layoutManager.getReverseLayout() ? span.mViews.get(span.mViews.size() - 1) : span.mViews.get(0); } @@ -926,7 +930,8 @@ public void onRefreshLayout(RecyclerView.State state, VirtualLayoutManager.Ancho if (BuildConfig.DEBUG) { Log.d(TAG, "onRefreshLayout span.clear()"); } - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.clear(); } } @@ -959,13 +964,15 @@ public void checkAnchorInfo(RecyclerView.State state, VirtualLayoutManager.Ancho if (BuildConfig.DEBUG) { Log.d(TAG, "checkAnchorInfo span.clear()"); } - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.clear(); span.setLine(anchorInfo.coordinate); } } else { int anchorPos = anchorInfo.layoutFromEnd ? Integer.MIN_VALUE : Integer.MAX_VALUE; - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; if (!span.mViews.isEmpty()) { if (anchorInfo.layoutFromEnd) { View view = span.mViews.get(span.mViews.size() - 1); @@ -1017,7 +1024,8 @@ public void checkAnchorInfo(RecyclerView.State state, VirtualLayoutManager.Ancho if (BuildConfig.DEBUG) { Log.d(TAG, "checkAnchorInfo span.cacheReferenceLineAndClear()"); } - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.cacheReferenceLineAndClear(helper.getReverseLayout() ^ anchorInfo.layoutFromEnd, offset, orientationHelper); } } @@ -1047,7 +1055,8 @@ public void onRestoreInstanceState(Bundle bundle) { public void onOffsetChildrenVertical(int dy, LayoutManagerHelper helper) { super.onOffsetChildrenVertical(dy, helper); if (helper.getOrientation() == VERTICAL) { - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.onOffset(dy); } } @@ -1057,7 +1066,8 @@ public void onOffsetChildrenVertical(int dy, LayoutManagerHelper helper) { public void onOffsetChildrenHorizontal(int dx, LayoutManagerHelper helper) { super.onOffsetChildrenHorizontal(dx, helper); if (helper.getOrientation() == HORIZONTAL) { - for (Span span : mSpans) { + for (int i = 0, size = mSpans.length; i < size; i++) { + Span span = mSpans[i]; span.onOffset(dx); } } From 967dceacb1b9b851d838cdd2a9d2c5f403eea978 Mon Sep 17 00:00:00 2001 From: MikeAfc Date: Thu, 3 May 2018 09:46:52 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Fix=20#342=EF=BC=8Cbackground=20color=20not?= =?UTF-8?q?=20work=20in=20StickyLayoutHelper.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/android/vlayout/layout/StickyLayoutHelper.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StickyLayoutHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StickyLayoutHelper.java index 8dad360d..cc35fecb 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StickyLayoutHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/layout/StickyLayoutHelper.java @@ -291,6 +291,10 @@ public void afterLayout(RecyclerView.Recycler recycler, RecyclerView.State state } else { fixLayoutStateInCase2(orientationHelper, recycler, startPosition, endPosition, helper); } + + if (mFixView != null){ + mFixView.setBackgroundColor(mBgColor); + } } private void fixLayoutStateFromAbnormal2Normal(OrientationHelperEx orientationHelper, RecyclerView.Recycler recycler, int startPosition, int endPosition, From bb7a288969f70f6632e7f693f4b51fa98771ec7c Mon Sep 17 00:00:00 2001 From: MikeAfc Date: Thu, 3 May 2018 11:05:45 +0800 Subject: [PATCH 4/7] Make recycle offset work whether layout from start or from end. --- .../alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java b/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java index 59ca1ca5..9e36a0f7 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java @@ -1155,8 +1155,7 @@ protected int fill(RecyclerView.Recycler recycler, LayoutState layoutState, } recycleByLayoutStateExpose(recycler, layoutState); } - int remainingSpace = layoutState.mAvailable + layoutState.mExtra + ( - layoutState.mLayoutDirection == LayoutState.LAYOUT_START ? 0 : recycleOffset); //FIXME opt here to fix bg and shake + int remainingSpace = layoutState.mAvailable + layoutState.mExtra + recycleOffset; while (remainingSpace > 0 && layoutState.hasMore(state)) { layoutChunkResultCache.resetInternal(); layoutChunk(recycler, state, layoutState, layoutChunkResultCache); From 56495651d8f7809f0cc04ace8ba2f341bb03f276 Mon Sep 17 00:00:00 2001 From: MikeAfc Date: Mon, 7 May 2018 19:29:26 +0800 Subject: [PATCH 5/7] Add ViewLifeCycleHelper and ViewLifeCycleListener to invoke correct view life cycle when call VirtualLayoutManager#setRecycleOffset. --- .../android/vlayout/VirtualLayoutManager.java | 24 ++- .../vlayout/extend/ViewLifeCycleHelper.java | 148 ++++++++++++++++++ .../vlayout/extend/ViewLifeCycleListener.java | 13 ++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java create mode 100644 vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleListener.java diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java index fa29833b..075dd827 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java @@ -32,6 +32,8 @@ import java.util.List; import java.util.Map; +import com.alibaba.android.vlayout.extend.ViewLifeCycleHelper; +import com.alibaba.android.vlayout.extend.ViewLifeCycleListener; import com.alibaba.android.vlayout.layout.BaseLayoutHelper; import com.alibaba.android.vlayout.layout.DefaultLayoutHelper; import com.alibaba.android.vlayout.layout.FixAreaAdjuster; @@ -94,6 +96,7 @@ public static void enableDebugging(boolean isDebug) { private int mMaxMeasureSize = -1; + private ViewLifeCycleHelper mViewLifeCycleHelper; public VirtualLayoutManager(@NonNull final Context context) { this(context, VERTICAL); @@ -435,6 +438,10 @@ private void runPostLayout(RecyclerView.Recycler recycler, RecyclerView.State st } } } + + if (null != mViewLifeCycleHelper) { + mViewLifeCycleHelper.checkViewStatusInScreen(); + } } } @@ -604,6 +611,21 @@ public void offsetChildrenVertical(int dy) { LayoutHelper layoutHelper = layoutHelpers.get(i); layoutHelper.onOffsetChildrenVertical(dy, this); } + + if (null != mViewLifeCycleHelper) { + mViewLifeCycleHelper.checkViewStatusInScreen(); + } + } + + public void setViewLifeCycleListener(@NonNull ViewLifeCycleListener viewLifeCycleListener) { + if (null == viewLifeCycleListener) { + throw new IllegalArgumentException("ViewLifeCycleListener should not be null!"); + } + mViewLifeCycleHelper = new ViewLifeCycleHelper(this, viewLifeCycleListener); + } + + public int getVirtualLayoutDirection() { + return mLayoutState.mLayoutDirection; } private LayoutStateWrapper mTempLayoutStateWrapper = new LayoutStateWrapper(); @@ -1399,7 +1421,7 @@ private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int if (getOrientation() == VERTICAL) { widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mDecorInsets.left, - lp.rightMargin + mDecorInsets.right); + lp.rightMargin + mDecorInsets.right); } if (getOrientation() == HORIZONTAL) { heightSpec = updateSpecWithExtra(heightSpec, mDecorInsets.top, diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java new file mode 100644 index 00000000..ef2a904c --- /dev/null +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java @@ -0,0 +1,148 @@ +package com.alibaba.android.vlayout.extend; + +import android.support.annotation.NonNull; +import android.support.v4.util.ArrayMap; +import android.view.View; + +import com.alibaba.android.vlayout.VirtualLayoutManager; + +public class ViewLifeCycleHelper { + public enum STATUS { + APPEARING, + APPEARED, + DISAPPEARING, + DISAPPEARED + } + + private ArrayMap mViewStatusMap = new ArrayMap<>(); + + private ViewLifeCycleListener mViewLifeCycleListener; + + private VirtualLayoutManager mVirtualLayoutManager; + + private int scrHeight; + + public ViewLifeCycleHelper(VirtualLayoutManager virtualLayoutManager, @NonNull ViewLifeCycleListener mViewLifeCycleListener) { + this.mViewLifeCycleListener = mViewLifeCycleListener; + this.mVirtualLayoutManager = virtualLayoutManager; + } + + public void checkViewStatusInScreen() { + for (int i = 0; i < mVirtualLayoutManager.getChildCount(); i++) { + View view = mVirtualLayoutManager.getChildAt(i); + if (scrHeight == 0) { + scrHeight = view.getContext().getResources().getDisplayMetrics().heightPixels; + } +// Log.e("huang", "checkViewStatusInScreen: view=" + view.getTag(R.id.tag_layout_helper_bg) + " rect=" + rect.toString()); + + if (mVirtualLayoutManager.getVirtualLayoutDirection() == VirtualLayoutManager.LayoutState.LAYOUT_END) { + if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyDisAppear(view)) { +// Log.e("huang", "LAYOUT_END checkViewDisAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + setViewDisappearing(view); + } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyAppear(view)) { +// Log.e("huang", "LAYOUT_END checkViewAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + setViewAppearing(view); + } + } else { + if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyAppear(view)) { +// Log.e("huang", "LAYOUT_START checkViewAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + setViewAppearing(view); + } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyDisAppear(view)) { +// Log.e("huang", "LAYOUT_START checkViewDisAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + setViewDisappearing(view); + } + } + + if (view.getTop() > 0 && view.getBottom() < scrHeight) { + // fully in screen + + if (isViewReadyAppear(view)) { + // if not appeared, call appear + setViewAppearing(view); +// Log.e("huang", "Add appear: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); + + } else { + if (getViewStatus(view) != STATUS.APPEARED) { + setViewAppeared(view); +// Log.e("huang", "Appeared: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); + } + } + } else if (view.getBottom() < 0 || view.getTop() > scrHeight) { + // not in screen + if (isViewReadyDisAppear(view)) { + // if not disappeared, call disappear + setViewDisappearing(view); +// Log.e("huang", "Add disappear:" + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); + + } else { + if (getViewStatus(view) != STATUS.DISAPPEARED) { + setViewDisappeared(view); +// Log.e("huang", "Disappeared: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); + } + } + } + } + } + + private STATUS getViewStatus(View view) { + if (!mViewStatusMap.containsKey(view)) { + mViewStatusMap.put(view, STATUS.DISAPPEARED); + return STATUS.DISAPPEARED; + } + return mViewStatusMap.get(view); + } + + private void setViewstatus(View view, STATUS status) { + mViewStatusMap.put(view, status); + } + + private boolean isViewReadyAppear(View view) { + return getViewStatus(view) == STATUS.DISAPPEARED; + } + + private void setViewAppearing(View view) { + if (getViewStatus(view) == STATUS.APPEARING) { + return; + } + + setViewstatus(view, STATUS.APPEARING); + if (null != mViewLifeCycleListener) { + mViewLifeCycleListener.onAppearing(view); + } + } + + private void setViewAppeared(View view) { + if (getViewStatus(view) == STATUS.APPEARED) { + return; + } + setViewstatus(view, STATUS.APPEARED); + if (null != mViewLifeCycleListener) { + mViewLifeCycleListener.onAppeared(view); + } + } + + private boolean isViewReadyDisAppear(View view) { + return getViewStatus(view) == STATUS.APPEARED; + } + + private void setViewDisappearing(View view) { + if (getViewStatus(view) == STATUS.DISAPPEARING) { + return; + } + + setViewstatus(view, STATUS.DISAPPEARING); + if (null != mViewLifeCycleListener) { + mViewLifeCycleListener.onDisappearing(view); + } + } + + private void setViewDisappeared(View view) { + if (getViewStatus(view) == STATUS.DISAPPEARED) { + return; + } + setViewstatus(view, STATUS.DISAPPEARED); + if (null != mViewLifeCycleListener) { + mViewLifeCycleListener.onDisappeared(view); + } + } +} diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleListener.java b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleListener.java new file mode 100644 index 00000000..21e12114 --- /dev/null +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleListener.java @@ -0,0 +1,13 @@ +package com.alibaba.android.vlayout.extend; + +import android.view.View; + +public interface ViewLifeCycleListener { + void onAppearing(View view); + + void onDisappearing(View view); + + void onAppeared(View view); + + void onDisappeared(View view); +} From 03634c0233e4c17d4bf65d293175beb32d426cee Mon Sep 17 00:00:00 2001 From: MikeAfc Date: Mon, 7 May 2018 20:00:03 +0800 Subject: [PATCH 6/7] Remove unused log --- .../vlayout/extend/ViewLifeCycleHelper.java | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java index ef2a904c..4c5bc5b2 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java @@ -33,22 +33,17 @@ public void checkViewStatusInScreen() { if (scrHeight == 0) { scrHeight = view.getContext().getResources().getDisplayMetrics().heightPixels; } -// Log.e("huang", "checkViewStatusInScreen: view=" + view.getTag(R.id.tag_layout_helper_bg) + " rect=" + rect.toString()); if (mVirtualLayoutManager.getVirtualLayoutDirection() == VirtualLayoutManager.LayoutState.LAYOUT_END) { - if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyDisAppear(view)) { -// Log.e("huang", "LAYOUT_END checkViewDisAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyDisAppearing(view)) { setViewDisappearing(view); - } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyAppear(view)) { -// Log.e("huang", "LAYOUT_END checkViewAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyAppearing(view)) { setViewAppearing(view); } } else { - if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyAppear(view)) { -// Log.e("huang", "LAYOUT_START checkViewAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyAppearing(view)) { setViewAppearing(view); - } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyDisAppear(view)) { -// Log.e("huang", "LAYOUT_START checkViewDisAppear: " + rect.toString() + "view=" + view.getTag(R.id.tag_layout_helper_bg)); + } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyDisAppearing(view)) { setViewDisappearing(view); } } @@ -56,30 +51,21 @@ public void checkViewStatusInScreen() { if (view.getTop() > 0 && view.getBottom() < scrHeight) { // fully in screen - if (isViewReadyAppear(view)) { - // if not appeared, call appear + if (isViewReadyAppearing(view)) { setViewAppearing(view); -// Log.e("huang", "Add appear: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); - } else { - if (getViewStatus(view) != STATUS.APPEARED) { - setViewAppeared(view); -// Log.e("huang", "Appeared: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); - } + } else if (isViewReadyAppeared(view)) { + setViewAppeared(view); } } else if (view.getBottom() < 0 || view.getTop() > scrHeight) { // not in screen - if (isViewReadyDisAppear(view)) { - // if not disappeared, call disappear + if (isViewReadyDisAppearing(view)) { setViewDisappearing(view); -// Log.e("huang", "Add disappear:" + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); - } else { - if (getViewStatus(view) != STATUS.DISAPPEARED) { - setViewDisappeared(view); -// Log.e("huang", "Disappeared: " + rect.toString() + " view=" + view.getTag(R.id.tag_layout_helper_bg)); - } + } else if (isViewReadyDisAppeared(view)) { + setViewDisappeared(view); } + } } } @@ -96,7 +82,7 @@ private void setViewstatus(View view, STATUS status) { mViewStatusMap.put(view, status); } - private boolean isViewReadyAppear(View view) { + private boolean isViewReadyAppearing(View view) { return getViewStatus(view) == STATUS.DISAPPEARED; } @@ -111,6 +97,10 @@ private void setViewAppearing(View view) { } } + private boolean isViewReadyAppeared(View view) { + return getViewStatus(view) == STATUS.APPEARING; + } + private void setViewAppeared(View view) { if (getViewStatus(view) == STATUS.APPEARED) { return; @@ -121,7 +111,7 @@ private void setViewAppeared(View view) { } } - private boolean isViewReadyDisAppear(View view) { + private boolean isViewReadyDisAppearing(View view) { return getViewStatus(view) == STATUS.APPEARED; } @@ -136,6 +126,10 @@ private void setViewDisappearing(View view) { } } + private boolean isViewReadyDisAppeared(View view) { + return getViewStatus(view) == STATUS.DISAPPEARING; + } + private void setViewDisappeared(View view) { if (getViewStatus(view) == STATUS.DISAPPEARED) { return; From ab5264fbf0db64508b236ee557f986993b1cd344 Mon Sep 17 00:00:00 2001 From: MikeAfc Date: Tue, 8 May 2018 10:44:18 +0800 Subject: [PATCH 7/7] Update demo --- .../vlayout/example/VLayoutActivity.java | 43 ++++++++++++++----- .../vlayout/ExposeLinearLayoutManagerEx.java | 2 +- .../android/vlayout/VirtualLayoutManager.java | 5 +++ .../vlayout/extend/ViewLifeCycleHelper.java | 12 +++--- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java b/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java index c6f49441..cc19dc5e 100644 --- a/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java +++ b/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java @@ -29,6 +29,7 @@ import com.alibaba.android.vlayout.RecyclablePagerAdapter; import com.alibaba.android.vlayout.VirtualLayoutManager; import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams; +import com.alibaba.android.vlayout.extend.ViewLifeCycleListener; import com.alibaba.android.vlayout.layout.ColumnLayoutHelper; import com.alibaba.android.vlayout.layout.FixLayoutHelper; import com.alibaba.android.vlayout.layout.FloatLayoutHelper; @@ -109,8 +110,8 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); - mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container); -; + mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); + ; mFirstText = (TextView) findViewById(R.id.first); mLastText = (TextView) findViewById(R.id.last); mCountText = (TextView) findViewById(R.id.count); @@ -135,8 +136,6 @@ public void onScrolled(RecyclerView recyclerView, int i, int i2) { } }); - layoutManager.setRecycleOffset(300); - recyclerView.setLayoutManager(layoutManager); // layoutManager.setReverseLayout(true); @@ -157,6 +156,31 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle viewPool.setMaxRecycledViews(0, 20); + layoutManager.setRecycleOffset(300); + + // viewLifeCycleListener should be used with setRecycleOffset() + layoutManager.setViewLifeCycleListener(new ViewLifeCycleListener() { + @Override + public void onAppearing(View view) { +// Log.e("ViewLifeCycleTest", "onAppearing: " + view); + } + + @Override + public void onDisappearing(View view) { +// Log.e("ViewLifeCycleTest", "onDisappearing: " + view); + } + + @Override + public void onAppeared(View view) { +// Log.e("ViewLifeCycleTest", "onAppeared: " + view); + } + + @Override + public void onDisappeared(View view) { +// Log.e("ViewLifeCycleTest", "onDisappeared: " + view); + } + }); + final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true); recyclerView.setAdapter(delegateAdapter); @@ -264,7 +288,7 @@ public void onBindViewHolder(MainViewHolder holder, int position) { }); } - { + { RangeGridLayoutHelper layoutHelper = new RangeGridLayoutHelper(4); layoutHelper.setBgColor(Color.GREEN); layoutHelper.setWeights(new float[]{20f, 26.665f}); @@ -339,7 +363,7 @@ public void onBindViewHolder(MainViewHolder holder, int position) { adapters.add(new SubAdapter(this, layoutHelper, 23)); } - + { SingleLayoutHelper layoutHelper = new SingleLayoutHelper(); layoutHelper.setBgColor(Color.BLUE); @@ -616,7 +640,7 @@ public void onBindViewHolder(MainViewHolder holder, int position) { } adapters.add( - new FooterAdapter(recyclerView, VLayoutActivity.this, new GridLayoutHelper(1), 1)); + new FooterAdapter(recyclerView, VLayoutActivity.this, new GridLayoutHelper(1), 1)); delegateAdapter.setAdapters(adapters); @@ -626,8 +650,8 @@ public void onBindViewHolder(MainViewHolder holder, int position) { trigger = new Runnable() { @Override public void run() { - //recyclerView.scrollToPosition(22); - //recyclerView.getAdapter().notifyDataSetChanged(); + //recyclerView.scrollToPosition(22); + //recyclerView.getAdapter().notifyDataSetChanged(); //mainHandler.postDelayed(trigger, 1000); //List newAdapters = new ArrayList<>(); //newAdapters.add((new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3))); @@ -658,7 +682,6 @@ public void onClick(View v) { }); - mainHandler.postDelayed(trigger, 1000); mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java b/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java index 9e36a0f7..b569c353 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/ExposeLinearLayoutManagerEx.java @@ -129,7 +129,7 @@ class ExposeLinearLayoutManagerEx extends LinearLayoutManager { private final Method mEnsureLayoutStateMethod; - private int recycleOffset; + protected int recycleOffset; /** * Creates a vertical LinearLayoutManager diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java index 075dd827..355cf969 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java @@ -621,6 +621,11 @@ public void setViewLifeCycleListener(@NonNull ViewLifeCycleListener viewLifeCycl if (null == viewLifeCycleListener) { throw new IllegalArgumentException("ViewLifeCycleListener should not be null!"); } + + if (recycleOffset == 0) { + throw new IllegalArgumentException("ViewLifeCycleListener should work with virtualLayoutManager.setRecycleOffset()!"); + } + mViewLifeCycleHelper = new ViewLifeCycleHelper(this, viewLifeCycleListener); } diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java index 4c5bc5b2..6a248d42 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/ViewLifeCycleHelper.java @@ -35,20 +35,20 @@ public void checkViewStatusInScreen() { } if (mVirtualLayoutManager.getVirtualLayoutDirection() == VirtualLayoutManager.LayoutState.LAYOUT_END) { - if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyDisAppearing(view)) { + if (view.getTop() <= 0 && view.getBottom() >= 0 && isViewReadyDisAppearing(view)) { setViewDisappearing(view); - } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyAppearing(view)) { + } else if (view.getTop() <= scrHeight && view.getBottom() >= scrHeight && isViewReadyAppearing(view)) { setViewAppearing(view); } } else { - if (view.getTop() < 0 && view.getBottom() > 0 && isViewReadyAppearing(view)) { + if (view.getTop() <= 0 && view.getBottom() >= 0 && isViewReadyAppearing(view)) { setViewAppearing(view); - } else if (view.getTop() < scrHeight && view.getBottom() > scrHeight && isViewReadyDisAppearing(view)) { + } else if (view.getTop() <= scrHeight && view.getBottom() >= scrHeight && isViewReadyDisAppearing(view)) { setViewDisappearing(view); } } - if (view.getTop() > 0 && view.getBottom() < scrHeight) { + if (view.getTop() >= 0 && view.getBottom() <= scrHeight) { // fully in screen if (isViewReadyAppearing(view)) { @@ -57,7 +57,7 @@ public void checkViewStatusInScreen() { } else if (isViewReadyAppeared(view)) { setViewAppeared(view); } - } else if (view.getBottom() < 0 || view.getTop() > scrHeight) { + } else if (view.getBottom() <= 0 || view.getTop() >= scrHeight) { // not in screen if (isViewReadyDisAppearing(view)) { setViewDisappearing(view);