Java Code Examples for android.support.v7.widget.RecyclerView#Recycler

The following examples show how to use android.support.v7.widget.RecyclerView#Recycler . 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: RangeStyle.java    From vlayout with MIT License 6 votes vote down vote up
public void beforeLayout(RecyclerView.Recycler recycler, RecyclerView.State state,
    LayoutManagerHelper helper) {
    if (!isChildrenEmpty()) {
        for (Map.Entry<Range<Integer>, T> entry : mChildren.entrySet()) {
            RangeStyle childRangeStyle = entry.getValue();
            childRangeStyle.beforeLayout(recycler, state, helper);
        }
    }
    if (requireLayoutView()) {
        if (mLayoutView != null) {
            // helper.detachChildView(mLayoutView);
        }
    } else {
        // if no layoutView is required, remove it
        if (mLayoutView != null) {
            if (mLayoutViewUnBindListener != null) {
                mLayoutViewUnBindListener.onUnbind(mLayoutView, getLayoutHelper());
            }
            helper.removeChildView(mLayoutView);
            mLayoutView = null;
        }
    }

}
 
Example 2
Source File: SimpleExpandModel.java    From ExpandLayoutManager with Apache License 2.0 6 votes vote down vote up
@Override
public boolean checkRemeasureNeeded(final ExpandLayoutManager expandLayoutManager, final RecyclerView.Recycler recycler, final RecyclerView.State state) {
    if (0 == state.getItemCount()) {
        expandLayoutManager.removeAndRecycleAllViews(recycler);
        return false;
    }

    if (null == mDecoratedChildHeight) {
        final AnimationView view = (AnimationView) recycler.getViewForPosition(0);
        view.resetState();
        expandLayoutManager.addView((View) view);
        expandLayoutManager.measureChildWithMargins((View) view, 0, 0);

        mDecoratedChildHeight = expandLayoutManager.getDecoratedMeasuredHeight((View) view);
        expandLayoutManager.removeAndRecycleView((View) view, recycler);

        return true;
    }
    return false;
}
 
Example 3
Source File: AllAppsGridAdapter.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler,
        RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info);

    ViewGroup.LayoutParams lp = host.getLayoutParams();
    AccessibilityNodeInfoCompat.CollectionItemInfoCompat cic = info.getCollectionItemInfo();
    if (!(lp instanceof LayoutParams) || (cic == null)) {
        return;
    }
    LayoutParams glp = (LayoutParams) lp;
    info.setCollectionItemInfo(AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain(
            cic.getRowIndex() - getRowsNotForAccessibility(glp.getViewAdapterPosition()),
            cic.getRowSpan(),
            cic.getColumnIndex(),
            cic.getColumnSpan(),
            cic.isHeading(),
            cic.isSelected()));
}
 
Example 4
Source File: FloatLayoutHelper.java    From vlayout with MIT License 5 votes vote down vote up
@Override
public void beforeLayout(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutManagerHelper helper) {
    super.beforeLayout(recycler, state, helper);

    if (mFixView != null && helper.isViewHolderUpdated(mFixView)) {
        // remove view, not recycle
        helper.removeChildView(mFixView);
        helper.recycleView(mFixView);
        mFixView.setOnTouchListener(null);
        mFixView = null;
    }

    mDoNormalHandle = false;
}
 
Example 5
Source File: GridLayoutManager.java    From TvRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if (dx == 0 || getChildCount() == 0) {
        return 0;
    }

    mInScroll = true;
    int realOffset = dx;
    int maxScrollSpace = getMaxScrollDistance();
    if (mHorizontalOffset + dx < 0) {
        if (Math.abs(dx) > mHorizontalOffset) {
            realOffset = -mHorizontalOffset;
        } else {
            realOffset -= mHorizontalOffset;
        }
    } else if (mItemsRect.size() >= getItemCount() && mHorizontalOffset + dx > maxScrollSpace) {
        realOffset = maxScrollSpace - mHorizontalOffset;
    }

    if (DEBUG) {
        Log.d(TAG, "scrollHorizontallyBy: dx=" + dx + "=realOffset=" + realOffset
                + "=HorizontalOffset=" + mHorizontalOffset +
                "=maxScrollSpace=" + maxScrollSpace);
    }
    if (mHorizontalOffset != 0) {
        recycleAndFillViews(recycler, state, dx);
    }
    mHorizontalOffset += realOffset;
    offsetChildrenHorizontal(-realOffset);
    mInScroll = false;
    return realOffset;
}
 
Example 6
Source File: RecyclerViewUtils.java    From UGank with GNU General Public License v3.0 5 votes vote down vote up
public static void setStaggeredGridLayoutManager(RecyclerView rv, int spanCount) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL) {
        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                Log.e(RecyclerViewUtils.TAG, "meet an IndexOutOfBoundsException in RecyclerView");
            }
        }
    };
    rv.setLayoutManager(staggeredGridLayoutManager);
}
 
Example 7
Source File: WrapContentLinearLayoutManager.java    From talk-android with MIT License 5 votes vote down vote up
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    try {
        super.onLayoutChildren(recycler, state);
    } catch (IndexOutOfBoundsException e) {
        Logger.e(TAG, "IOOBE in RecyclerView", e);
    }
}
 
Example 8
Source File: RecyclerViewUtils.java    From LLApp with Apache License 2.0 5 votes vote down vote up
public static void setGridLayout(RecyclerView rv, int spanCount) {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(rv.getContext(), spanCount, GridLayoutManager.VERTICAL, false) {
        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                Log.e(TAG, "meet an IndexOutOfBoundsException in RecyclerView");
            }
        }
    };
    rv.setLayoutManager(gridLayoutManager);
}
 
Example 9
Source File: BaseLayoutManager.java    From KinoCast with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
                                RecyclerView.State state) {
    if (mOrientation == VERTICAL) {
        return 0;
    }
    return scrollBy(dx, recycler, state);
}
 
Example 10
Source File: VirtualLayoutManager.java    From vlayout with MIT License 5 votes vote down vote up
@Override
public void detachAndScrapViewAt(int index, RecyclerView.Recycler recycler) {
    View child = getChildAt(index);
    RecyclerView.ViewHolder holder = getChildViewHolder(child);
    if (holder instanceof CacheViewHolder && ((CacheViewHolder) holder).needCached()) {
        // mark not invalid
        ViewHolderWrapper.setFlags(holder, 0, FLAG_INVALID);
    }

    super.detachAndScrapViewAt(index, recycler);
}
 
Example 11
Source File: WrapContentLinearLayoutManager.java    From android-auto-call-recorder with MIT License 5 votes vote down vote up
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    try {
        super.onLayoutChildren(recycler, state);
    } catch (IndexOutOfBoundsException e) {
        Log.e("probe", "meet a IOOBE in RecyclerView");
    }
}
 
Example 12
Source File: PagerLayoutManager.java    From YCScrollPager with Apache License 2.0 5 votes vote down vote up
/**
 * 监听竖直方向的相对偏移量
 * @param dy                                y轴滚动值
 * @param recycler                          recycler
 * @param state                             state滚动状态
 * @return                                  int值
 */
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
                              RecyclerView.State state) {
    //需要判断子类的数量不能为0的情况
    if (getChildCount() == 0 || dy == 0) {
        return 0;
    }
    this.mDrift = dy;
    return super.scrollVerticallyBy(dy, recycler, state);
}
 
Example 13
Source File: RxLinearLayoutManager.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    try {
        super.onLayoutChildren(recycler, state);
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: StaticGridLayoutManager.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if (getChildCount() == 0) {
        return 0;
    }

    //Take top measurements from the top-left child
    final View topView = getChildAt(0);
    //Take bottom measurements from the bottom-right child.
    final View bottomView = getChildAt(getChildCount()-1);

    //Optimize the case where the entire data set is too small to scroll
    int viewSpan = getDecoratedBottom(bottomView) - getDecoratedTop(topView);
    if (viewSpan <= getVerticalSpace()) {
        //We cannot scroll in either direction
        return 0;
    }

    int delta;
    int maxRowCount = getTotalRowCount();
    boolean topBoundReached = getFirstVisibleRow() == 0;
    boolean bottomBoundReached = getLastVisibleRow() >= maxRowCount;
    if (dy > 0) { // Contents are scrolling up
        //Check against bottom bound
        if (bottomBoundReached) {
            //If we've reached the last row, enforce limits
            int bottomOffset;
            if (rowOfIndex(getChildCount() - 1) >= (maxRowCount - 1)) {
                //We are truly at the bottom, determine how far
                bottomOffset = getVerticalSpace() - getDecoratedBottom(bottomView)
                        + getPaddingBottom();
            } else {
                /*
                 * Extra space added to account for allowing bottom space in the grid.
                 * This occurs when the overlap in the last row is not large enough to
                 * ensure that at least one element in that row isn't fully recycled.
                 */
                bottomOffset = getVerticalSpace() - (getDecoratedBottom(bottomView)
                        + mDecoratedChildHeight) + getPaddingBottom();
            }

            delta = Math.max(-dy, bottomOffset);
        } else {
            //No limits while the last row isn't visible
            delta = -dy;
        }
    } else { // Contents are scrolling down
        //Check against top bound
        if (topBoundReached) {
            int topOffset = -getDecoratedTop(topView) + getPaddingTop();

            delta = Math.min(-dy, topOffset);
        } else {
            delta = -dy;
        }
    }

    offsetChildrenVertical(delta);

    if (dy > 0) {
        if (getDecoratedBottom(topView) < 0 && !bottomBoundReached) {
            fillGrid(DIRECTION_DOWN, recycler);
        } else if (!bottomBoundReached) {
            fillGrid(DIRECTION_NONE, recycler);
        }
    } else {
        if (getDecoratedTop(topView) > 0 && !topBoundReached) {
            fillGrid(DIRECTION_UP, recycler);
        } else if (!topBoundReached) {
            fillGrid(DIRECTION_NONE, recycler);
        }
    }

    /*
     * Return value determines if a boundary has been reached
     * (for edge effects and flings). If returned value does not
     * match original delta (passed in), RecyclerView will draw
     * an edge effect.
     */
    return -delta;
}
 
Example 15
Source File: ScalableGridLayoutManager.java    From MultiView with Apache License 2.0 4 votes vote down vote up
@Override
public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) {
    super.onDetachedFromWindow(view, recycler);
    mRecyclerView = null;
}
 
Example 16
Source File: VirtualLayoutManagerTest.java    From vlayout with MIT License 4 votes vote down vote up
void after(RecyclerView.Recycler recycler, RecyclerView.State state) {
}
 
Example 17
Source File: FullyLinearLayoutManager.java    From MyHearts with Apache License 2.0 4 votes vote down vote up
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                      int widthSpec, int heightSpec) {

    final int widthMode = View.MeasureSpec.getMode(widthSpec);
    final int heightMode = View.MeasureSpec.getMode(heightSpec);
    final int widthSize = View.MeasureSpec.getSize(widthSpec);
    final int heightSize = View.MeasureSpec.getSize(heightSpec);

    Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
            + " \nheightMode " + heightSpec
            + " \nwidthSize " + widthSize
            + " \nheightSize " + heightSize
            + " \ngetItemCount() " + getItemCount());

    int width = 0;
    int height = 0;
    for (int i = 0; i < getItemCount(); i++) {
        measureScrapChild(recycler, i,
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                mMeasuredDimension);

        if (getOrientation() == HORIZONTAL) {
            width = width + mMeasuredDimension[0];
            if (i == 0) {
                height = mMeasuredDimension[1];
            }
        } else {
            height = height + mMeasuredDimension[1];
            if (i == 0) {
                width = mMeasuredDimension[0];
            }
        }
    }
    switch (widthMode) {
        case View.MeasureSpec.EXACTLY:
            width = widthSize;
        case View.MeasureSpec.AT_MOST:
        case View.MeasureSpec.UNSPECIFIED:
    }

    switch (heightMode) {
        case View.MeasureSpec.EXACTLY:
            height = heightSize;
        case View.MeasureSpec.AT_MOST:
        case View.MeasureSpec.UNSPECIFIED:
    }

    setMeasuredDimension(width, height);
}
 
Example 18
Source File: StaticGridLayoutManager.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    //We have nothing to show for an empty data set but clear any existing views
    if (getItemCount() == 0) {
        detachAndScrapAttachedViews(recycler);
        return;
    }

    //Make the grid as square as possible, column count is root of the data set
    mTotalColumnCount = (int) Math.round(Math.sqrt(getItemCount()));

    if (getChildCount() == 0) { //First or empty layout
        //Scrap measure one child
        View scrap = recycler.getViewForPosition(0);
        addView(scrap);
        measureChildWithMargins(scrap, 0, 0);

        /*
         * We make some assumptions in this code based on every child
         * view being the same size (i.e. a uniform grid). This allows
         * us to compute the following values up front because they
         * won't change.
         */
        mDecoratedChildWidth = getDecoratedMeasuredWidth(scrap);
        mDecoratedChildHeight = getDecoratedMeasuredHeight(scrap);

        detachAndScrapView(scrap, recycler);
    }

    //Always update the visible row/column counts
    updateWindowSizing();

    int childLeft;
    int childTop;
    if (getChildCount() == 0) { //First or empty layout
        /*
         * Reset the visible and scroll positions
         */
        mFirstVisiblePosition = 0;
        childLeft = childTop = 0;
    } else if (getVisibleChildCount() > getItemCount()) {
        //Data set is too small to scroll fully, just reset position
        mFirstVisiblePosition = 0;
        childLeft = childTop = 0;
    } else { //Adapter data set changes
        /*
         * Keep the existing initial position, and save off
         * the current scrolled offset.
         */
        final View topChild = getChildAt(0);
        if (mForceClearOffsets) {
            childLeft = childTop = 0;
            mForceClearOffsets = false;
        } else {
            childLeft = getDecoratedLeft(topChild);
            childTop = getDecoratedTop(topChild);
        }

        /*
         * Adjust the visible position if out of bounds in the
         * new layout. This occurs when the new item count in an adapter
         * is much smaller than it was before, and you are scrolled to
         * a location where no items would exist.
         */
        int lastVisiblePosition = positionOfIndex(getVisibleChildCount() - 1);
        if (lastVisiblePosition >= getItemCount()) {
            lastVisiblePosition = (getItemCount() - 1);
            int lastColumn = mVisibleColumnCount - 1;
            int lastRow = mVisibleRowCount - 1;

            //Adjust to align the last position in the bottom-right
            mFirstVisiblePosition = Math.max(
                    lastVisiblePosition - lastColumn - (lastRow * getTotalColumnCount()), 0);

            childLeft = getHorizontalSpace() - (mDecoratedChildWidth * mVisibleColumnCount);
            childTop = getVerticalSpace() - (mDecoratedChildHeight * mVisibleRowCount);

            //Correct cases where shifting to the bottom-right overscrolls the top-left
            // This happens on data sets too small to scroll in a direction.
            if (getFirstVisibleRow() == 0) {
                childTop = Math.min(childTop, 0);
            }
            if (getFirstVisibleColumn() == 0) {
                childLeft = Math.min(childLeft, 0);
            }
        }
    }

    //Clear all attached views into the recycle bin
    detachAndScrapAttachedViews(recycler);

    //Fill the grid for the initial layout of views
    fillGrid(DIRECTION_NONE, childLeft, childTop, recycler);
}
 
Example 19
Source File: GridLayoutManagerTV.java    From Android-tv-widget with Apache License 2.0 4 votes vote down vote up
@Override
public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
    View nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state);
    return null;
}
 
Example 20
Source File: CarouselLayoutManager.java    From CarouselLayoutManager with Apache License 2.0 4 votes vote down vote up
@Override
public void onMeasure(final RecyclerView.Recycler recycler, final RecyclerView.State state, final int widthSpec, final int heightSpec) {
    mDecoratedChildSizeInvalid = true;

    super.onMeasure(recycler, state, widthSpec, heightSpec);
}