Search in sources :

Example 31 with OrientationHelperEx

use of com.alibaba.android.vlayout.OrientationHelperEx in project vlayout by alibaba.

the class StaggeredGridLayoutHelper method checkForGaps.

/**
 * check whether there are gaps that need to be fixed
 */
private void checkForGaps() {
    if (mLayoutManager == null) {
        return;
    }
    final VirtualLayoutManager layoutManager = mLayoutManager.get();
    if (layoutManager == null || layoutManager.getChildCount() == 0) {
        return;
    }
    final Range<Integer> range = getRange();
    // align position, which should check gap for
    final int minPos, maxPos, alignPos;
    if (layoutManager.getReverseLayout()) {
        minPos = layoutManager.findLastVisibleItemPosition();
        maxPos = layoutManager.findFirstVisibleItemPosition();
        alignPos = range.getUpper() - 1;
    } else {
        minPos = layoutManager.findFirstVisibleItemPosition();
        maxPos = layoutManager.findLastCompletelyVisibleItemPosition();
        alignPos = range.getLower();
    }
    final OrientationHelperEx orientationHelper = layoutManager.getMainOrientationHelper();
    final int childCount = layoutManager.getChildCount();
    int viewAnchor = Integer.MIN_VALUE;
    int alignLine = Integer.MIN_VALUE;
    // find view anchor and get align line, the views should be aligned to alignLine
    if (layoutManager.getReverseLayout()) {
        for (int i = childCount - 1; i >= 0; i--) {
            View view = layoutManager.getChildAt(i);
            int position = layoutManager.getPosition(view);
            if (position == alignPos) {
                viewAnchor = position;
                if (i == childCount - 1) {
                    // if last child, alignLine is the end of child
                    alignLine = orientationHelper.getDecoratedEnd(view);
                } else {
                    // if not, alignLine is the start of next child
                    View child = layoutManager.getChildAt(i + 1);
                    int aPos = layoutManager.getPosition(child);
                    if (aPos == position - 1) {
                        // if position is sequence, which means the next child is not hidden one
                        alignLine = orientationHelper.getDecoratedStart(child) - layoutManager.obtainExtraMargin(child, false) + layoutManager.obtainExtraMargin(view, true);
                    } else {
                        // if next child is hidden one, use end of current view
                        alignLine = orientationHelper.getDecoratedEnd(view);
                    }
                }
                break;
            }
        }
    } else {
        for (int i = 0; i < childCount; i++) {
            View view = layoutManager.getChildAt(i);
            int position = layoutManager.getPosition(view);
            if (position == alignPos) {
                viewAnchor = position;
                if (i == 0) {
                    // TODO: there is problem
                    // if first child, alignLine is the start
                    alignLine = orientationHelper.getDecoratedStart(view);
                } else {
                    // if not, alignLine is the end of previous child
                    View child = layoutManager.getChildAt(i - 1);
                    alignLine = orientationHelper.getDecoratedEnd(child) + layoutManager.obtainExtraMargin(child, true, false) - layoutManager.obtainExtraMargin(view, false, false);
                    int viewStart = orientationHelper.getDecoratedStart(view);
                    if (alignLine == viewStart) {
                        // actually not gap here skip;
                        viewAnchor = Integer.MIN_VALUE;
                    } else {
                        int nextPosition = layoutManager.getPosition(child);
                        if (nextPosition != alignPos - 1) {
                            // may has sticky layout, add extra space occur by stickyLayoutHelper
                            LayoutHelper layoutHelper = layoutManager.findLayoutHelperByPosition(alignPos - 1);
                            if (layoutHelper != null && layoutHelper instanceof StickyLayoutHelper) {
                                if (layoutHelper.getFixedView() != null) {
                                    alignLine += layoutHelper.getFixedView().getMeasuredHeight();
                                }
                            }
                        } else {
                            LayoutHelper layoutHelper = layoutManager.findLayoutHelperByPosition(nextPosition);
                            layoutHelper.getRange();
                        }
                    }
                }
                break;
            }
        }
    }
    if (viewAnchor == Integer.MIN_VALUE) {
        // if not find view anchor, break
        return;
    }
    View gapView = hasGapsToFix(layoutManager, viewAnchor, alignLine);
    if (gapView != null) {
        if (mSpans != null) {
            for (int i = 0, size = mSpans.length; i < size; i++) {
                Span span = mSpans[i];
                span.setLine(alignLine);
            }
        }
        layoutManager.requestSimpleAnimationsInNextLayout();
        layoutManager.requestLayout();
    }
}
Also used : OrientationHelperEx(com.alibaba.android.vlayout.OrientationHelperEx) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 32 with OrientationHelperEx

use of com.alibaba.android.vlayout.OrientationHelperEx in project vlayout by alibaba.

the class BaseLayoutHelper method adjustLayout.

@Override
public void adjustLayout(int startPosition, int endPosition, LayoutManagerHelper helper) {
    if (requireLayoutView()) {
        View refer = null;
        Rect tempRect = new Rect();
        final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
        for (int i = 0; i < helper.getChildCount(); i++) {
            refer = helper.getChildAt(i);
            int anchorPos = helper.getPosition(refer);
            if (getRange().contains(anchorPos)) {
                if (refer.getVisibility() == View.GONE) {
                    tempRect.setEmpty();
                } else {
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) refer.getLayoutParams();
                    if (helper.getOrientation() == VirtualLayoutManager.VERTICAL) {
                        tempRect.union(helper.getDecoratedLeft(refer) - params.leftMargin, orientationHelper.getDecoratedStart(refer), helper.getDecoratedRight(refer) + params.rightMargin, orientationHelper.getDecoratedEnd(refer));
                    } else {
                        tempRect.union(orientationHelper.getDecoratedStart(refer), helper.getDecoratedTop(refer) - params.topMargin, orientationHelper.getDecoratedEnd(refer), helper.getDecoratedBottom(refer) + params.bottomMargin);
                    }
                }
            }
        }
        if (!tempRect.isEmpty()) {
            mLayoutRegion.set(tempRect.left - mPaddingLeft, tempRect.top - mPaddingTop, tempRect.right + mPaddingRight, tempRect.bottom + mPaddingBottom);
        } else {
            mLayoutRegion.setEmpty();
        }
        if (mLayoutView != null) {
            mLayoutView.layout(mLayoutRegion.left, mLayoutRegion.top, mLayoutRegion.right, mLayoutRegion.bottom);
        }
    }
}
Also used : Rect(android.graphics.Rect) OrientationHelperEx(com.alibaba.android.vlayout.OrientationHelperEx) RecyclerView(android.support.v7.widget.RecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

OrientationHelperEx (com.alibaba.android.vlayout.OrientationHelperEx)32 RecyclerView (android.support.v7.widget.RecyclerView)26 View (android.view.View)26 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)18 LayoutParams (com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams)10 LayoutParams (android.support.v7.widget.RecyclerView.LayoutParams)5 Rect (android.graphics.Rect)2 ArrayMap (android.support.v4.util.ArrayMap)1 SimpleArrayMap (android.support.v4.util.SimpleArrayMap)1 ViewGroup (android.view.ViewGroup)1 LayoutParams (android.view.ViewGroup.LayoutParams)1 TextView (android.widget.TextView)1 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)1 Range (com.alibaba.android.vlayout.Range)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1