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 cde8451e..3201b7f1 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/RangeLayoutHelperFinder.java @@ -27,10 +27,13 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import java.util.Arrays; 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 @@ -46,6 +49,8 @@ public class RangeLayoutHelperFinder extends LayoutHelperFinder { @NonNull private List mReverseLayoutHelpers =new LinkedList<>(); + private LayoutHelperItem[] mSortedLayoutHelpers = null; + @NonNull private Comparator mLayoutHelperItemComparator = new Comparator() { @Override @@ -68,16 +73,21 @@ public void setLayouts(@Nullable List layouts) { mReverseLayoutHelpers.clear(); mLayoutHelperItems.clear(); if (layouts != null) { - for (int i = 0, size = layouts.size(); i < size; i++) { - LayoutHelper helper = layouts.get(i); + ListIterator iterator = layouts.listIterator(); + LayoutHelper helper = null; + while (iterator.hasNext()) { + helper = iterator.next(); mLayoutHelpers.add(helper); mLayoutHelperItems.add(new LayoutHelperItem(helper)); } - for (int i = layouts.size() - 1; i >= 0; i--) { - mReverseLayoutHelpers.add(layouts.get(i)); + + while (iterator.hasPrevious()) { + mReverseLayoutHelpers.add(iterator.previous()); } - Collections.sort(mLayoutHelperItems, mLayoutHelperItemComparator); + // Collections.sort(mLayoutHelperItems, mLayoutHelperItemComparator); + mSortedLayoutHelpers = mLayoutHelperItems.toArray(new LayoutHelperItem[mLayoutHelperItems.size()]); + Arrays.sort(mSortedLayoutHelpers, mLayoutHelperItemComparator); } } @@ -90,18 +100,17 @@ protected List getLayoutHelpers() { @Nullable @Override public LayoutHelper getLayoutHelper(int position) { - final int count = mLayoutHelperItems.size(); - if (count == 0) { + if (mSortedLayoutHelpers == null || mSortedLayoutHelpers.length == 0) { return null; } + final int count = mSortedLayoutHelpers.length; int s = 0, e = count - 1, m; LayoutHelperItem rs = null; - // binary search range while (s <= e) { m = (s + e) / 2; - rs = mLayoutHelperItems.get(m); + rs = mSortedLayoutHelpers[m]; if (rs.getStartPosition() > position) { e = m - 1; } else if (rs.getEndPosition() < position) { 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 13a9c96d..7653b3e7 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java @@ -47,12 +47,14 @@ import android.view.ViewGroup; import android.view.ViewParent; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.ListIterator; import java.util.Map; @@ -107,6 +109,20 @@ public static void enableDebugging(boolean isDebug) { private ViewLifeCycleHelper mViewLifeCycleHelper; + private Comparator, Integer>> mRangeComparator = new Comparator, Integer>>() { + @Override + public int compare(Pair, Integer> a, Pair, Integer> b) { + if (a == null && b == null) return 0; + if (a == null) return -1; + if (b == null) return 1; + + Range lr = a.first; + Range rr = b.first; + + return lr.getLower() - rr.getLower(); + } + }; + public VirtualLayoutManager(@NonNull final Context context) { this(context, VERTICAL); } @@ -175,9 +191,12 @@ public void setHelperFinder(@NonNull final LayoutHelperFinder finder) { List helpers = new LinkedList<>(); if (this.mHelperFinder != null) { List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper helper = layoutHelpers.get(i); - helpers.add(helper); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); + helpers.add(layoutHelper); + } } @@ -211,17 +230,18 @@ public void setFixOffset(int left, int top, int right, int bottom) { */ public void setLayoutHelpers(@Nullable List helpers) { List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper helper = layoutHelpers.get(i); + Iterator it0 = layoutHelpers.iterator(); + while (it0.hasNext()) { + LayoutHelper helper = it0.next(); oldHelpersSet.put(System.identityHashCode(helper), helper); } // set ranges if (helpers != null) { int start = 0; - for (int i = 0; i < helpers.size(); i++) { - LayoutHelper helper = helpers.get(i); - + Iterator it1 = helpers.iterator(); + while (it1.hasNext()) { + LayoutHelper helper = it1.next(); if (helper instanceof FixAreaLayoutHelper) { ((FixAreaLayoutHelper) helper).setAdjuster(mFixAreaAdjustor); } @@ -244,12 +264,12 @@ public void setLayoutHelpers(@Nullable List helpers) { this.mHelperFinder.setLayouts(helpers); layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper helper = layoutHelpers.get(i); - newHelpersSet.put(System.identityHashCode(helper), helper); + Iterator iterator = layoutHelpers.iterator(); + while (iterator.hasNext()) { + LayoutHelper layoutHelper = iterator.next(); + newHelpersSet.put(System.identityHashCode(layoutHelper), layoutHelper); } - for (Iterator> it = oldHelpersSet.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = it.next(); Integer key = entry.getKey(); @@ -364,8 +384,10 @@ public void onAnchorReady(RecyclerView.State state, ExposeLinearLayoutManagerEx. mTempAnchorInfoWrapper.position = anchorInfo.mPosition; mTempAnchorInfoWrapper.coordinate = anchorInfo.mCoordinate; List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.onRefreshLayout(state, mTempAnchorInfoWrapper, this); } } @@ -434,8 +456,10 @@ private void runPreLayout(RecyclerView.Recycler recycler, RecyclerView.State sta if (mNested == 0) { List reverseLayoutHelpers = mHelperFinder.reverse(); - for (int i = 0, size = reverseLayoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = reverseLayoutHelpers.get(i); + Iterator iterator = reverseLayoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.beforeLayout(recycler, state, this); } } @@ -450,8 +474,10 @@ private void runPostLayout(RecyclerView.Recycler recycler, RecyclerView.State st final int startPosition = findFirstVisibleItemPosition(); final int endPosition = findLastVisibleItemPosition(); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); try { layoutHelper.afterLayout(recycler, state, startPosition, endPosition, scrolled, this); } catch (Exception e) { @@ -608,8 +634,10 @@ public void onScrollStateChanged(int state) { int startPosition = findFirstVisibleItemPosition(); int endPosition = findLastVisibleItemPosition(); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.onScrollStateChanged(state, startPosition, endPosition, this); } } @@ -619,9 +647,12 @@ public void offsetChildrenHorizontal(int dx) { super.offsetChildrenHorizontal(dx); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.onOffsetChildrenHorizontal(dx, this); + } } @@ -629,8 +660,10 @@ public void offsetChildrenHorizontal(int dx) { public void offsetChildrenVertical(int dy) { super.offsetChildrenVertical(dy); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.onOffsetChildrenVertical(dy, this); } @@ -657,7 +690,7 @@ public int getVirtualLayoutDirection() { private LayoutStateWrapper mTempLayoutStateWrapper = new LayoutStateWrapper(); - private List, Integer>> mRangeLengths = new LinkedList<>(); + private List, Integer>> mRangeLengths = new ArrayList<>(); @Nullable private int findRangeLength(@NonNull final Range range) { @@ -732,19 +765,7 @@ protected void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State st } mRangeLengths.add(Pair.create(range, consumed)); - Collections.sort(mRangeLengths, new Comparator, Integer>>() { - @Override - public int compare(Pair, Integer> a, Pair, Integer> b) { - if (a == null && b == null) return 0; - if (a == null) return -1; - if (b == null) return 1; - - Range lr = a.first; - Range rr = b.first; - - return lr.getLower() - rr.getLower(); - } - }); + Collections.sort(mRangeLengths, mRangeComparator); } } @@ -852,8 +873,10 @@ public void onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCo @Override public void onItemsChanged(RecyclerView recyclerView) { List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.onItemsChanged(this); } @@ -907,8 +930,10 @@ public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycl super.onDetachedFromWindow(view, recycler); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); layoutHelper.clear(this); } @@ -1141,8 +1166,10 @@ public List getFixedViews() { // TODO: support zIndex? List views = new LinkedList<>(); List layoutHelpers = mHelperFinder.getLayoutHelpers(); - for (int i = 0, size = layoutHelpers.size(); i < size; i++) { - LayoutHelper layoutHelper = layoutHelpers.get(i); + Iterator iterator = layoutHelpers.iterator(); + LayoutHelper layoutHelper = null; + while (iterator.hasNext()) { + layoutHelper = iterator.next(); View fixedView = layoutHelper.getFixedView(); if (fixedView != null) { views.add(fixedView);