Java Code Examples for android.support.v7.widget.RecyclerView.State#getItemCount()

The following examples show how to use android.support.v7.widget.RecyclerView.State#getItemCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
protected int getAnchorItemPosition(State state) {
    final int itemCount = state.getItemCount();

    int pendingPosition = getPendingScrollPosition();
    if (pendingPosition != RecyclerView.NO_POSITION) {
        if (pendingPosition < 0 || pendingPosition >= itemCount) {
            pendingPosition = RecyclerView.NO_POSITION;
        }
    }

    if (pendingPosition != RecyclerView.NO_POSITION) {
        return pendingPosition;
    } else if (getChildCount() > 0) {
        return findFirstValidChildPosition(itemCount);
    } else {
        return 0;
    }
}
 
Example 2
Source File: ScrollbarHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
static int computeScrollOffset(State state, OrientationHelper orientation, View startChild, View endChild, LayoutManager lm, boolean smoothScrollbarEnabled, boolean reverseLayout) {
    if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null || endChild == null) {
        return 0;
    }
    int itemsBefore = reverseLayout ? Math.max(0, (state.getItemCount() - Math.max(lm.getPosition(startChild), lm.getPosition(endChild))) - 1) : Math.max(0, Math.min(lm.getPosition(startChild), lm.getPosition(endChild)));
    if (!smoothScrollbarEnabled) {
        return itemsBefore;
    }
    return Math.round((((float) itemsBefore) * (((float) Math.abs(orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild))) / ((float) (Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1)))) + ((float) (orientation.getStartAfterPadding() - orientation.getDecoratedStart(startChild))));
}
 
Example 3
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void correctTooHigh(int childCount, Recycler recycler, State state) {
    // First see if the last item is visible. If it is not, it is OK for the
    // top of the list to be pushed up.
    final int lastPosition = getLastVisiblePosition();
    if (lastPosition != state.getItemCount() - 1 || childCount == 0) {
        return;
    }

    // This is bottom of our drawable area.
    final int start = getStartWithPadding();
    final int end = getEndWithPadding();
    final int firstPosition = getFirstVisiblePosition();

    // This is how far the end edge of the last view is from the end of the
    // drawable area.
    int endOffset = end - mLayoutEnd;

    // Make sure we are 1) Too high, and 2) Either there are more rows above the
    // first row or the first row is scrolled off the top of the drawable area
    if (endOffset > 0 && (firstPosition > 0 || mLayoutStart < start))  {
        if (firstPosition == 0) {
            // Don't pull the top too far down.
            endOffset = Math.min(endOffset, start - mLayoutStart);
        }

        // Move everything down
        offsetChildren(endOffset);

        if (firstPosition > 0) {
            // Fill the gap that was opened above first position with more
            // children, if possible.
            fillBefore(firstPosition - 1, recycler);

            // Close up the remaining gap.
            adjustViewsStartOrEnd();
        }
    }
}
 
Example 4
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void fillSpecific(int position, Recycler recycler, State state) {
    if (state.getItemCount() == 0) {
        return;
    }

    makeAndAddView(position, Direction.END, recycler);

    final int extraSpaceBefore;
    final int extraSpaceAfter;

    final int extraSpace = getExtraLayoutSpace(state);
    if (state.getTargetScrollPosition() < position) {
        extraSpaceAfter = 0;
        extraSpaceBefore = extraSpace;
    } else {
        extraSpaceAfter = extraSpace;
        extraSpaceBefore = 0;
    }

    fillBefore(position - 1, recycler, extraSpaceBefore);

    // This will correct for the top of the first view not
    // touching the top of the parent.
    adjustViewsStartOrEnd();

    fillAfter(position + 1, recycler, state, extraSpaceAfter);
    correctTooHigh(getChildCount(), recycler, state);
}
 
Example 5
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void fillAfter(int position, Recycler recycler, State state, int extraSpace) {
    final int limit = getEndWithPadding() + extraSpace;

    final int itemCount = state.getItemCount();
    while (canAddMoreViews(Direction.END, limit) && position < itemCount) {
        makeAndAddView(position, Direction.END, recycler);
        position++;
    }
}
 
Example 6
Source File: BaseLayoutManager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onLayoutChildren(Recycler recycler, State state) {
    final boolean restoringLanes = (mLanesToRestore != null);
    if (restoringLanes) {
        mLanes = mLanesToRestore;
        mItemEntries = mItemEntriesToRestore;

        mLanesToRestore = null;
        mItemEntriesToRestore = null;
    }

    final boolean refreshingLanes = ensureLayoutState();

    // Still not able to create lanes, nothing we can do here,
    // just bail for now.
    if (mLanes == null) {
        return;
    }

    final int itemCount = state.getItemCount();
    mItemEntries.setAdapterSize(itemCount);

    final int anchorItemPosition = getAnchorItemPosition(state);

    // Only move layout if we're not restoring a layout state.
    if (anchorItemPosition > 0 && (refreshingLanes || !restoringLanes)) {
        moveLayoutToPosition(anchorItemPosition, getPendingScrollOffset(), recycler, state);
    }

    mLanes.reset(Direction.START);

    super.onLayoutChildren(recycler, state);
}
 
Example 7
Source File: GridLayoutManager.java    From letv with Apache License 2.0 5 votes vote down vote up
void onAnchorReady(Recycler recycler, State state, AnchorInfo anchorInfo, int itemDirection) {
    super.onAnchorReady(recycler, state, anchorInfo, itemDirection);
    updateMeasurements();
    if (state.getItemCount() > 0 && !state.isPreLayout()) {
        ensureAnchorIsInCorrectSpan(recycler, state, anchorInfo, itemDirection);
    }
    ensureViewSet();
}
 
Example 8
Source File: GridLayoutManager.java    From letv with Apache License 2.0 5 votes vote down vote up
public int getColumnCountForAccessibility(Recycler recycler, State state) {
    if (this.mOrientation == 1) {
        return this.mSpanCount;
    }
    if (state.getItemCount() < 1) {
        return 0;
    }
    return getSpanGroupIndex(recycler, state, state.getItemCount() - 1) + 1;
}
 
Example 9
Source File: ScrollbarHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
static int computeScrollRange(State state, OrientationHelper orientation, View startChild, View endChild, LayoutManager lm, boolean smoothScrollbarEnabled) {
    if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null || endChild == null) {
        return 0;
    }
    if (!smoothScrollbarEnabled) {
        return state.getItemCount();
    }
    return (int) ((((float) (orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild))) / ((float) (Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1))) * ((float) state.getItemCount()));
}
 
Example 10
Source File: ScrollbarHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
static int computeScrollExtent(State state, OrientationHelper orientation, View startChild, View endChild, LayoutManager lm, boolean smoothScrollbarEnabled) {
    if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null || endChild == null) {
        return 0;
    }
    if (!smoothScrollbarEnabled) {
        return Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1;
    }
    return Math.min(orientation.getTotalSpace(), orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild));
}
 
Example 11
Source File: StaggeredGridLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
private void onLayoutChildren(Recycler recycler, State state, boolean shouldCheckForGaps) {
    boolean needToCheckForGaps = true;
    ensureOrientationHelper();
    AnchorInfo anchorInfo = this.mAnchorInfo;
    anchorInfo.reset();
    if (!(this.mPendingSavedState == null && this.mPendingScrollPosition == -1) && state.getItemCount() == 0) {
        removeAndRecycleAllViews(recycler);
        return;
    }
    if (this.mPendingSavedState != null) {
        applyPendingSavedState(anchorInfo);
    } else {
        resolveShouldLayoutReverse();
        anchorInfo.mLayoutFromEnd = this.mShouldReverseLayout;
    }
    updateAnchorInfoForLayout(state, anchorInfo);
    if (this.mPendingSavedState == null && !(anchorInfo.mLayoutFromEnd == this.mLastLayoutFromEnd && isLayoutRTL() == this.mLastLayoutRTL)) {
        this.mLazySpanLookup.clear();
        anchorInfo.mInvalidateOffsets = true;
    }
    if (getChildCount() > 0 && (this.mPendingSavedState == null || this.mPendingSavedState.mSpanOffsetsSize < 1)) {
        int i;
        if (anchorInfo.mInvalidateOffsets) {
            for (i = 0; i < this.mSpanCount; i++) {
                this.mSpans[i].clear();
                if (anchorInfo.mOffset != Integer.MIN_VALUE) {
                    this.mSpans[i].setLine(anchorInfo.mOffset);
                }
            }
        } else {
            for (i = 0; i < this.mSpanCount; i++) {
                this.mSpans[i].cacheReferenceLineAndClear(this.mShouldReverseLayout, anchorInfo.mOffset);
            }
        }
    }
    detachAndScrapAttachedViews(recycler);
    this.mLayoutState.mRecycle = false;
    this.mLaidOutInvalidFullSpan = false;
    updateMeasureSpecs(this.mSecondaryOrientation.getTotalSpace());
    updateLayoutState(anchorInfo.mPosition, state);
    if (anchorInfo.mLayoutFromEnd) {
        setLayoutStateDirection(-1);
        fill(recycler, this.mLayoutState, state);
        setLayoutStateDirection(1);
        this.mLayoutState.mCurrentPosition = anchorInfo.mPosition + this.mLayoutState.mItemDirection;
        fill(recycler, this.mLayoutState, state);
    } else {
        setLayoutStateDirection(1);
        fill(recycler, this.mLayoutState, state);
        setLayoutStateDirection(-1);
        this.mLayoutState.mCurrentPosition = anchorInfo.mPosition + this.mLayoutState.mItemDirection;
        fill(recycler, this.mLayoutState, state);
    }
    repositionToWrapContentIfNecessary();
    if (getChildCount() > 0) {
        if (this.mShouldReverseLayout) {
            fixEndGap(recycler, state, true);
            fixStartGap(recycler, state, false);
        } else {
            fixStartGap(recycler, state, true);
            fixEndGap(recycler, state, false);
        }
    }
    boolean hasGaps = false;
    if (shouldCheckForGaps && !state.isPreLayout()) {
        if (this.mGapStrategy == 0 || getChildCount() <= 0 || (!this.mLaidOutInvalidFullSpan && hasGapsToFix() == null)) {
            needToCheckForGaps = false;
        }
        if (needToCheckForGaps) {
            removeCallbacks(this.mCheckForGapsRunnable);
            if (checkForGaps()) {
                hasGaps = true;
            }
        }
        this.mPendingScrollPosition = -1;
        this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
    }
    this.mLastLayoutFromEnd = anchorInfo.mLayoutFromEnd;
    this.mLastLayoutRTL = isLayoutRTL();
    this.mPendingSavedState = null;
    if (hasGaps) {
        onLayoutChildren(recycler, state, false);
    }
}
 
Example 12
Source File: StaggeredGridLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
boolean updateAnchorFromPendingData(State state, AnchorInfo anchorInfo) {
    boolean z = false;
    if (state.isPreLayout() || this.mPendingScrollPosition == -1) {
        return false;
    }
    if (this.mPendingScrollPosition < 0 || this.mPendingScrollPosition >= state.getItemCount()) {
        this.mPendingScrollPosition = -1;
        this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
        return false;
    } else if (this.mPendingSavedState == null || this.mPendingSavedState.mAnchorPosition == -1 || this.mPendingSavedState.mSpanOffsetsSize < 1) {
        View child = findViewByPosition(this.mPendingScrollPosition);
        if (child != null) {
            anchorInfo.mPosition = this.mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition();
            if (this.mPendingScrollPositionOffset != Integer.MIN_VALUE) {
                if (anchorInfo.mLayoutFromEnd) {
                    anchorInfo.mOffset = (this.mPrimaryOrientation.getEndAfterPadding() - this.mPendingScrollPositionOffset) - this.mPrimaryOrientation.getDecoratedEnd(child);
                    return true;
                }
                anchorInfo.mOffset = (this.mPrimaryOrientation.getStartAfterPadding() + this.mPendingScrollPositionOffset) - this.mPrimaryOrientation.getDecoratedStart(child);
                return true;
            } else if (this.mPrimaryOrientation.getDecoratedMeasurement(child) > this.mPrimaryOrientation.getTotalSpace()) {
                anchorInfo.mOffset = anchorInfo.mLayoutFromEnd ? this.mPrimaryOrientation.getEndAfterPadding() : this.mPrimaryOrientation.getStartAfterPadding();
                return true;
            } else {
                int startGap = this.mPrimaryOrientation.getDecoratedStart(child) - this.mPrimaryOrientation.getStartAfterPadding();
                if (startGap < 0) {
                    anchorInfo.mOffset = -startGap;
                    return true;
                }
                int endGap = this.mPrimaryOrientation.getEndAfterPadding() - this.mPrimaryOrientation.getDecoratedEnd(child);
                if (endGap < 0) {
                    anchorInfo.mOffset = endGap;
                    return true;
                }
                anchorInfo.mOffset = Integer.MIN_VALUE;
                return true;
            }
        }
        anchorInfo.mPosition = this.mPendingScrollPosition;
        if (this.mPendingScrollPositionOffset == Integer.MIN_VALUE) {
            if (calculateScrollDirectionForPosition(anchorInfo.mPosition) == 1) {
                z = true;
            }
            anchorInfo.mLayoutFromEnd = z;
            anchorInfo.assignCoordinateFromPadding();
        } else {
            anchorInfo.assignCoordinateFromPadding(this.mPendingScrollPositionOffset);
        }
        anchorInfo.mInvalidateOffsets = true;
        return true;
    } else {
        anchorInfo.mOffset = Integer.MIN_VALUE;
        anchorInfo.mPosition = this.mPendingScrollPosition;
        return true;
    }
}
 
Example 13
Source File: LinearLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
private boolean isViewValidAsAnchor(View child, State state) {
    LayoutParams lp = (LayoutParams) child.getLayoutParams();
    return !lp.isItemRemoved() && lp.getViewLayoutPosition() >= 0 && lp.getViewLayoutPosition() < state.getItemCount();
}
 
Example 14
Source File: LinearLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
boolean hasMore(State state) {
    return this.mCurrentPosition >= 0 && this.mCurrentPosition < state.getItemCount();
}
 
Example 15
Source File: LinearLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
private void updateAnchorInfoForLayout(Recycler recycler, State state, AnchorInfo anchorInfo) {
    if (!updateAnchorFromPendingData(state, anchorInfo) && !updateAnchorFromChildren(recycler, state, anchorInfo)) {
        anchorInfo.assignCoordinateFromPadding();
        anchorInfo.mPosition = this.mStackFromEnd ? state.getItemCount() - 1 : 0;
    }
}
 
Example 16
Source File: LinearLayoutManager.java    From letv with Apache License 2.0 4 votes vote down vote up
private boolean updateAnchorFromPendingData(State state, AnchorInfo anchorInfo) {
    boolean z = false;
    if (state.isPreLayout() || this.mPendingScrollPosition == -1) {
        return false;
    }
    if (this.mPendingScrollPosition < 0 || this.mPendingScrollPosition >= state.getItemCount()) {
        this.mPendingScrollPosition = -1;
        this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
        return false;
    }
    anchorInfo.mPosition = this.mPendingScrollPosition;
    if (this.mPendingSavedState != null && this.mPendingSavedState.hasValidAnchor()) {
        anchorInfo.mLayoutFromEnd = this.mPendingSavedState.mAnchorLayoutFromEnd;
        if (anchorInfo.mLayoutFromEnd) {
            anchorInfo.mCoordinate = this.mOrientationHelper.getEndAfterPadding() - this.mPendingSavedState.mAnchorOffset;
            return true;
        }
        anchorInfo.mCoordinate = this.mOrientationHelper.getStartAfterPadding() + this.mPendingSavedState.mAnchorOffset;
        return true;
    } else if (this.mPendingScrollPositionOffset == Integer.MIN_VALUE) {
        View child = findViewByPosition(this.mPendingScrollPosition);
        if (child == null) {
            if (getChildCount() > 0) {
                boolean z2;
                if (this.mPendingScrollPosition < getPosition(getChildAt(0))) {
                    z2 = true;
                } else {
                    z2 = false;
                }
                if (z2 == this.mShouldReverseLayout) {
                    z = true;
                }
                anchorInfo.mLayoutFromEnd = z;
            }
            anchorInfo.assignCoordinateFromPadding();
            return true;
        } else if (this.mOrientationHelper.getDecoratedMeasurement(child) > this.mOrientationHelper.getTotalSpace()) {
            anchorInfo.assignCoordinateFromPadding();
            return true;
        } else if (this.mOrientationHelper.getDecoratedStart(child) - this.mOrientationHelper.getStartAfterPadding() < 0) {
            anchorInfo.mCoordinate = this.mOrientationHelper.getStartAfterPadding();
            anchorInfo.mLayoutFromEnd = false;
            return true;
        } else if (this.mOrientationHelper.getEndAfterPadding() - this.mOrientationHelper.getDecoratedEnd(child) < 0) {
            anchorInfo.mCoordinate = this.mOrientationHelper.getEndAfterPadding();
            anchorInfo.mLayoutFromEnd = true;
            return true;
        } else {
            anchorInfo.mCoordinate = anchorInfo.mLayoutFromEnd ? this.mOrientationHelper.getDecoratedEnd(child) + this.mOrientationHelper.getTotalSpaceChange() : this.mOrientationHelper.getDecoratedStart(child);
            return true;
        }
    } else {
        anchorInfo.mLayoutFromEnd = this.mShouldReverseLayout;
        if (this.mShouldReverseLayout) {
            anchorInfo.mCoordinate = this.mOrientationHelper.getEndAfterPadding() - this.mPendingScrollPositionOffset;
            return true;
        }
        anchorInfo.mCoordinate = this.mOrientationHelper.getStartAfterPadding() + this.mPendingScrollPositionOffset;
        return true;
    }
}
 
Example 17
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public int computeVerticalScrollRange(State state) {
    return state.getItemCount();
}
 
Example 18
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public int computeHorizontalScrollRange(State state) {
    return state.getItemCount();
}
 
Example 19
Source File: TwoWayLayoutManager.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
private void correctTooLow(int childCount, Recycler recycler, State state) {
    // First see if the first item is visible. If it is not, it is OK for the
    // end of the list to be pushed forward.
    final int firstPosition = getFirstVisiblePosition();
    if (firstPosition != 0 || childCount == 0) {
        return;
    }

    final int start = getStartWithPadding();
    final int end = getEndWithPadding();
    final int itemCount = state.getItemCount();
    final int lastPosition = getLastVisiblePosition();

    // This is how far the start edge of the first view is from the start of the
    // drawable area.
    int startOffset = mLayoutStart - start;

    // Make sure we are 1) Too low, and 2) Either there are more columns/rows below the
    // last column/row or the last column/row is scrolled off the end of the
    // drawable area.
    if (startOffset > 0) {
        if (lastPosition < itemCount - 1 || mLayoutEnd > end)  {
            if (lastPosition == itemCount - 1) {
                // Don't pull the bottom too far up.
                startOffset = Math.min(startOffset, mLayoutEnd - end);
            }

            // Move everything up.
            offsetChildren(-startOffset);

            if (lastPosition < itemCount - 1) {
                // Fill the gap that was opened below the last position with more
                // children, if possible.
                fillAfter(lastPosition + 1, recycler, state);

                // Close up the remaining gap.
                adjustViewsStartOrEnd();
            }
        } else if (lastPosition == itemCount - 1) {
            adjustViewsStartOrEnd();
        }
    }
}
 
Example 20
Source File: LayoutState.java    From letv with Apache License 2.0 4 votes vote down vote up
boolean hasMore(State state) {
    return this.mCurrentPosition >= 0 && this.mCurrentPosition < state.getItemCount();
}