Search in sources :

Example 1 with AbstractPositionIterator

use of com.beloo.widget.chipslayoutmanager.layouter.AbstractPositionIterator in project ChipsLayoutManager by BelooS.

the class ChipsLayoutManager method fillWithLayouter.

/**
     * place views in layout started from chosen position with chosen layouter
     */
private void fillWithLayouter(RecyclerView.Recycler recycler, ILayouter layouter, int startingPos) {
    if (startingPos < 0)
        return;
    AbstractPositionIterator iterator = layouter.positionIterator();
    iterator.move(startingPos);
    while (iterator.hasNext()) {
        int pos = iterator.next();
        View view = viewCache.get(pos);
        if (view == null) {
            // we don't have view from previous layouter stage, request new one
            try {
                view = recycler.getViewForPosition(pos);
            } catch (IndexOutOfBoundsException e) {
                /* WTF sometimes on prediction animation playing in case very fast sequential changes in adapter
                     * {@link #getItemCount} could return value bigger than real count of items
                     * & {@link RecyclerView.Recycler#getViewForPosition(int)} throws exception in this case!
                     * to handle it, just leave the loop*/
                break;
            }
            logger.onItemRequested();
            if (!layouter.placeView(view)) {
                /* reached end of visible bounds, exit.
                    recycle view, which was requested previously
                     */
                recycler.recycleView(view);
                logger.onItemRecycled();
                break;
            }
        } else {
            //we have detached views from previous layouter stage, attach it if needed
            if (!layouter.onAttachView(view)) {
                break;
            }
            //remove reattached view from cache
            viewCache.remove(pos);
        }
    }
    logger.onFinishedLayouter();
    //layout last row, in case iterator fully processed
    layouter.layoutRow();
}
Also used : AbstractPositionIterator(com.beloo.widget.chipslayoutmanager.layouter.AbstractPositionIterator) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 AbstractPositionIterator (com.beloo.widget.chipslayoutmanager.layouter.AbstractPositionIterator)1