Search in sources :

Example 6 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project Carbon by ZieIony.

the class IconTextListItemActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listcomponent);
    Samples.initToolbar(this, getString(R.string.iconTextListItemActivity_title));
    RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
    recycler.setLayoutManager(new LinearLayoutManager(this));
    RowListAdapter adapter = new RowListAdapter<>(DefaultIconTextItem.class, IconTextRow::new);
    adapter.addFactory(DefaultIconSearchItem.class, parent -> new IconSearchRow(parent, new ArraySearchDataProvider(new String[] {}), filterResults -> {
    }));
    recycler.setAdapter(adapter);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(new ColorDrawable(Carbon.getThemeColor(this, R.attr.carbon_dividerColor)), getResources().getDimensionPixelSize(R.dimen.carbon_1dip));
    dividerItemDecoration.setDrawRules(position -> position == 0);
    recycler.addItemDecoration(dividerItemDecoration);
    VectorDrawable drawable = new VectorDrawable(getResources(), R.raw.ic_face_24px);
    adapter.setItems(Arrays.asList(new DefaultIconSearchItem(this), new DefaultIconTextItem(drawable, "text"), new DefaultIconTextItem(drawable, "text"), new DefaultIconTextItem(drawable, "text"), new DefaultIconTextItem(drawable, "text")));
}
Also used : Arrays(java.util.Arrays) Bundle(android.os.Bundle) IconSearchRow(carbon.component.IconSearchRow) DividerItemDecoration(carbon.recycler.DividerItemDecoration) IconTextRow(carbon.component.IconTextRow) ColorDrawable(android.graphics.drawable.ColorDrawable) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DefaultIconTextItem(carbon.component.DefaultIconTextItem) DefaultIconSearchItem(carbon.component.DefaultIconSearchItem) Carbon(carbon.Carbon) RecyclerView(carbon.widget.RecyclerView) SamplesActivity(tk.zielony.carbonsamples.SamplesActivity) VectorDrawable(carbon.drawable.VectorDrawable) R(tk.zielony.carbonsamples.R) RowListAdapter(carbon.recycler.RowListAdapter) Nullable(android.support.annotation.Nullable) ArraySearchDataProvider(carbon.widget.ArraySearchDataProvider) Samples(tk.zielony.carbonsamples.Samples) DefaultIconSearchItem(carbon.component.DefaultIconSearchItem) ColorDrawable(android.graphics.drawable.ColorDrawable) IconTextRow(carbon.component.IconTextRow) RowListAdapter(carbon.recycler.RowListAdapter) RecyclerView(carbon.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DividerItemDecoration(carbon.recycler.DividerItemDecoration) DefaultIconTextItem(carbon.component.DefaultIconTextItem) IconSearchRow(carbon.component.IconSearchRow) VectorDrawable(carbon.drawable.VectorDrawable) ArraySearchDataProvider(carbon.widget.ArraySearchDataProvider)

Example 7 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project Carbon by ZieIony.

the class ImageTextSubtextDateListItemActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listcomponent);
    Samples.initToolbar(this, getString(R.string.imageTextSubtextDateListItemActivity_title));
    RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
    recycler.setLayoutManager(new LinearLayoutManager(this));
    RowListAdapter adapter = new RowListAdapter<>(DefaultImageTextSubtextDateItem.class, ImageTextSubtextDateRow::new);
    adapter.addFactory(DefaultHeaderItem.class, PaddedHeaderRow.FACTORY);
    recycler.setAdapter(adapter);
    Drawable drawable = getResources().getDrawable(R.drawable.watermelon);
    String date = format.format(new Date().getTime());
    adapter.setItems(Arrays.asList(new DefaultHeaderItem("Header"), new DefaultImageTextSubtextDateItem(drawable, "text", "subtext", date), new DefaultImageTextSubtextDateItem(drawable, "text", "subtext", date), new DefaultHeaderItem("Header"), new DefaultImageTextSubtextDateItem(drawable, "text", "subtext", date), new DefaultImageTextSubtextDateItem(drawable, "text", "subtext", date)));
}
Also used : DefaultHeaderItem(carbon.component.DefaultHeaderItem) ImageTextSubtextDateRow(carbon.component.ImageTextSubtextDateRow) Drawable(android.graphics.drawable.Drawable) RowListAdapter(carbon.recycler.RowListAdapter) RecyclerView(carbon.widget.RecyclerView) DefaultImageTextSubtextDateItem(carbon.component.DefaultImageTextSubtextDateItem) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Date(java.util.Date)

Example 8 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project GestureViews by alexvasilkov.

the class EndlessRecyclerAdapter method loadNextItemsIfNeeded.

private void loadNextItemsIfNeeded(RecyclerView recyclerView) {
    if (!isLoading && !isError) {
        View lastVisibleChild = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
        int lastVisiblePos = recyclerView.getChildAdapterPosition(lastVisibleChild);
        int total = getItemCount();
        if (lastVisiblePos >= total - loadingOffset) {
            // We need to use runnable, since recycler view does not like when we are notifying
            // about changes during scroll callback.
            recyclerView.post(new Runnable() {

                @Override
                public void run() {
                    loadNextItems();
                }
            });
        }
    }
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 9 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project android-parallax-recyclerview by kanytu.

the class HeaderLayoutManagerFixed method fill.

/**
     * The magic functions :). Fills the given layout, defined by the renderState. This is fairly
     * independent from the rest of the {@link android.support.v7.widget.LinearLayoutManager}
     * and with little change, can be made publicly available as a helper class.
     *
     * @param recycler        Current recycler that is attached to RecyclerView
     * @param renderState     Configuration on how we should fill out the available space.
     * @param state           Context passed by the RecyclerView to control scroll steps.
     * @param stopOnFocusable If true, filling stops in the first focusable new child
     * @return Number of pixels that it added. Useful for scoll functions.
     */
private int fill(RecyclerView.Recycler recycler, RenderState renderState, RecyclerView.State state, boolean stopOnFocusable) {
    // max offset we should set is mFastScroll + available
    final int start = renderState.mAvailable;
    if (renderState.mScrollingOffset != RenderState.SCOLLING_OFFSET_NaN) {
        // TODO ugly bug fix. should not happen
        if (renderState.mAvailable < 0) {
            renderState.mScrollingOffset += renderState.mAvailable;
        }
        recycleByRenderState(recycler, renderState);
    }
    int remainingSpace = renderState.mAvailable + renderState.mExtra + mHeaderIncrementFixer;
    while (remainingSpace > 0 && renderState.hasMore(state)) {
        View view = renderState.next(recycler);
        if (view == null) {
            if (DEBUG && renderState.mScrapList == null) {
                throw new RuntimeException("received null view when unexpected");
            }
            // no more items to layout.
            break;
        }
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
        if (!params.isItemRemoved() && mRenderState.mScrapList == null) {
            if (mShouldReverseLayout == (renderState.mLayoutDirection == RenderState.LAYOUT_START)) {
                addView(view);
            } else {
                addView(view, 0);
            }
        }
        measureChildWithMargins(view, 0, 0);
        int consumed = mOrientationHelper.getDecoratedMeasurement(view);
        int left, top, right, bottom;
        if (mOrientation == VERTICAL) {
            if (isLayoutRTL()) {
                right = getWidth() - getPaddingRight();
                left = right - mOrientationHelper.getDecoratedMeasurementInOther(view);
            } else {
                left = getPaddingLeft();
                right = left + mOrientationHelper.getDecoratedMeasurementInOther(view);
            }
            if (renderState.mLayoutDirection == RenderState.LAYOUT_START) {
                bottom = renderState.mOffset;
                top = renderState.mOffset - consumed;
            } else {
                top = renderState.mOffset;
                bottom = renderState.mOffset + consumed;
            }
        } else {
            top = getPaddingTop();
            bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view);
            if (renderState.mLayoutDirection == RenderState.LAYOUT_START) {
                right = renderState.mOffset;
                left = renderState.mOffset - consumed;
            } else {
                left = renderState.mOffset;
                right = renderState.mOffset + consumed;
            }
        }
        // We calculate everything with View's bounding box (which includes decor and margins)
        // To calculate correct layout position, we subtract margins.
        layoutDecorated(view, left + params.leftMargin, top + params.topMargin, right - params.rightMargin, bottom - params.bottomMargin);
        if (DEBUG) {
            Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" + (right - params.rightMargin) + ", b:" + (bottom - params.bottomMargin));
        }
        renderState.mOffset += consumed * renderState.mLayoutDirection;
        if (!params.isItemRemoved()) {
            renderState.mAvailable -= consumed;
            // we keep a separate remaining space because mAvailable is important for recycling
            remainingSpace -= consumed;
        }
        if (renderState.mScrollingOffset != RenderState.SCOLLING_OFFSET_NaN) {
            renderState.mScrollingOffset += consumed;
            if (renderState.mAvailable < 0) {
                renderState.mScrollingOffset += renderState.mAvailable;
            }
            recycleByRenderState(recycler, renderState);
        }
        if (stopOnFocusable && view.isFocusable()) {
            break;
        }
        if (state != null && state.getTargetScrollPosition() == getPosition(view)) {
            break;
        }
    }
    if (DEBUG) {
        validateChildOrder();
    }
    return start - renderState.mAvailable;
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 10 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project android-parallax-recyclerview by kanytu.

the class HeaderLayoutManagerFixed method onLayoutChildren.

/**
     * {@inheritDoc}
     */
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    // create render state
    if (DEBUG) {
        Log.d(TAG, "is pre layout:" + state.isPreLayout());
    }
    if (mPendingSavedState != null) {
        setOrientation(mPendingSavedState.mOrientation);
        setReverseLayout(mPendingSavedState.mReverseLayout);
        setStackFromEnd(mPendingSavedState.mStackFromEnd);
        mPendingScrollPosition = mPendingSavedState.mAnchorPosition;
    }
    ensureRenderState();
    // resolve layout direction
    resolveShouldLayoutReverse();
    // validate scroll position if exists
    if (mPendingScrollPosition != RecyclerView.NO_POSITION) {
        // validate it
        if (mPendingScrollPosition < 0 || mPendingScrollPosition >= state.getItemCount()) {
            mPendingScrollPosition = RecyclerView.NO_POSITION;
            mPendingScrollPositionOffset = INVALID_OFFSET;
            if (DEBUG) {
                Log.e(TAG, "ignoring invalid scroll position " + mPendingScrollPosition);
            }
        }
    }
    // this value might be updated if there is a target scroll position without an offset
    boolean layoutFromEnd = mShouldReverseLayout ^ mStackFromEnd;
    final boolean stackFromEndChanged = mLastStackFromEnd != mStackFromEnd;
    int anchorCoordinate, anchorItemPosition;
    if (mPendingScrollPosition != RecyclerView.NO_POSITION) {
        // if child is visible, try to make it a reference child and ensure it is fully visible.
        // if child is not visible, align it depending on its virtual position.
        anchorItemPosition = mPendingScrollPosition;
        if (mPendingSavedState != null) {
            // Anchor offset depends on how that child was laid out. Here, we update it
            // according to our current view bounds
            layoutFromEnd = mPendingSavedState.mAnchorLayoutFromEnd;
            if (layoutFromEnd) {
                anchorCoordinate = mOrientationHelper.getEndAfterPadding() - mPendingSavedState.mAnchorOffset;
            } else {
                anchorCoordinate = mOrientationHelper.getStartAfterPadding() + mPendingSavedState.mAnchorOffset;
            }
        } else if (mPendingScrollPositionOffset == INVALID_OFFSET) {
            View child = findViewByPosition(mPendingScrollPosition);
            if (child != null) {
                final int startGap = mOrientationHelper.getDecoratedStart(child) - mOrientationHelper.getStartAfterPadding();
                final int endGap = mOrientationHelper.getEndAfterPadding() - mOrientationHelper.getDecoratedEnd(child);
                final int childSize = mOrientationHelper.getDecoratedMeasurement(child);
                if (childSize > mOrientationHelper.getTotalSpace()) {
                    // item does not fit. fix depending on layout direction
                    anchorCoordinate = layoutFromEnd ? mOrientationHelper.getEndAfterPadding() : mOrientationHelper.getStartAfterPadding();
                } else if (startGap < 0) {
                    anchorCoordinate = mOrientationHelper.getStartAfterPadding();
                    layoutFromEnd = false;
                } else if (endGap < 0) {
                    anchorCoordinate = mOrientationHelper.getEndAfterPadding();
                    layoutFromEnd = true;
                } else {
                    anchorCoordinate = layoutFromEnd ? mOrientationHelper.getDecoratedEnd(child) : mOrientationHelper.getDecoratedStart(child);
                }
            } else {
                // item is not visible.
                if (getChildCount() > 0) {
                    // get position of any child, does not matter
                    int pos = getPosition(getChildAt(0));
                    if (mPendingScrollPosition < pos == mShouldReverseLayout) {
                        anchorCoordinate = mOrientationHelper.getEndAfterPadding();
                        layoutFromEnd = true;
                    } else {
                        anchorCoordinate = mOrientationHelper.getStartAfterPadding();
                        layoutFromEnd = false;
                    }
                } else {
                    anchorCoordinate = layoutFromEnd ? mOrientationHelper.getEndAfterPadding() : mOrientationHelper.getStartAfterPadding();
                }
            }
        } else {
            // override layout from end values for consistency
            if (mShouldReverseLayout) {
                anchorCoordinate = mOrientationHelper.getEndAfterPadding() - mPendingScrollPositionOffset;
                layoutFromEnd = true;
            } else {
                anchorCoordinate = mOrientationHelper.getStartAfterPadding() + mPendingScrollPositionOffset;
                layoutFromEnd = false;
            }
        }
    } else if (getChildCount() > 0 && !stackFromEndChanged) {
        if (layoutFromEnd) {
            View referenceChild = getChildClosestToEnd();
            anchorCoordinate = mOrientationHelper.getDecoratedEnd(referenceChild);
            anchorItemPosition = getPosition(referenceChild);
        } else {
            View referenceChild = getChildClosestToStart();
            anchorCoordinate = mOrientationHelper.getDecoratedStart(referenceChild);
            anchorItemPosition = getPosition(referenceChild);
        }
    } else {
        anchorCoordinate = layoutFromEnd ? mOrientationHelper.getEndAfterPadding() : mOrientationHelper.getStartAfterPadding();
        anchorItemPosition = mStackFromEnd ? state.getItemCount() - 1 : 0;
    }
    detachAndScrapAttachedViews(recycler);
    final int extraForStart;
    final int extraForEnd;
    final int extra = getExtraLayoutSpace(state);
    boolean before = state.getTargetScrollPosition() < anchorItemPosition;
    if (before == mShouldReverseLayout) {
        extraForEnd = extra;
        extraForStart = 0;
    } else {
        extraForStart = extra;
        extraForEnd = 0;
    }
    // first fill towards start
    updateRenderStateToFillStart(anchorItemPosition, anchorCoordinate);
    mRenderState.mExtra = extraForStart;
    if (!layoutFromEnd) {
        mRenderState.mCurrentPosition += mRenderState.mItemDirection;
    }
    fill(recycler, mRenderState, state, false);
    int startOffset = mRenderState.mOffset;
    // fill towards end
    updateRenderStateToFillEnd(anchorItemPosition, anchorCoordinate);
    mRenderState.mExtra = extraForEnd;
    if (layoutFromEnd) {
        mRenderState.mCurrentPosition += mRenderState.mItemDirection;
    }
    fill(recycler, mRenderState, state, false);
    int endOffset = mRenderState.mOffset;
    // changes may cause gaps on the UI, try to fix them.
    if (getChildCount() > 0) {
        // find which side we should check for gaps.
        if (mShouldReverseLayout ^ mStackFromEnd) {
            int fixOffset = fixLayoutEndGap(endOffset, recycler, state, true);
            startOffset += fixOffset;
            endOffset += fixOffset;
            fixOffset = fixLayoutStartGap(startOffset, recycler, state, false);
            startOffset += fixOffset;
            endOffset += fixOffset;
        } else {
            int fixOffset = fixLayoutStartGap(startOffset, recycler, state, true);
            startOffset += fixOffset;
            endOffset += fixOffset;
            fixOffset = fixLayoutEndGap(endOffset, recycler, state, false);
            startOffset += fixOffset;
            endOffset += fixOffset;
        }
    }
    // another view out of bounds.
    if (getChildCount() > 0 && !state.isPreLayout() && supportsPredictiveItemAnimations()) {
        // to make the logic simpler, we calculate the size of children and call fill.
        int scrapExtraStart = 0, scrapExtraEnd = 0;
        final List<RecyclerView.ViewHolder> scrapList = recycler.getScrapList();
        final int scrapSize = scrapList.size();
        final int firstChildPos = getPosition(getChildAt(0));
        for (int i = 0; i < scrapSize; i++) {
            RecyclerView.ViewHolder scrap = scrapList.get(i);
            final int position = scrap.getPosition();
            final int direction = position < firstChildPos != mShouldReverseLayout ? RenderState.LAYOUT_START : RenderState.LAYOUT_END;
            if (direction == RenderState.LAYOUT_START) {
                scrapExtraStart += mOrientationHelper.getDecoratedMeasurement(scrap.itemView);
            } else {
                scrapExtraEnd += mOrientationHelper.getDecoratedMeasurement(scrap.itemView);
            }
        }
        if (DEBUG) {
            Log.d(TAG, "for unused scrap, decided to add " + scrapExtraStart + " towards start and " + scrapExtraEnd + " towards end");
        }
        mRenderState.mScrapList = scrapList;
        if (scrapExtraStart > 0) {
            View anchor = getChildClosestToStart();
            updateRenderStateToFillStart(getPosition(anchor), startOffset);
            mRenderState.mExtra = scrapExtraStart;
            mRenderState.mAvailable = 0;
            mRenderState.mCurrentPosition += mShouldReverseLayout ? 1 : -1;
            fill(recycler, mRenderState, state, false);
        }
        if (scrapExtraEnd > 0) {
            View anchor = getChildClosestToEnd();
            updateRenderStateToFillEnd(getPosition(anchor), endOffset);
            mRenderState.mExtra = scrapExtraEnd;
            mRenderState.mAvailable = 0;
            mRenderState.mCurrentPosition += mShouldReverseLayout ? -1 : 1;
            fill(recycler, mRenderState, state, false);
        }
        mRenderState.mScrapList = null;
    }
    mPendingScrollPosition = RecyclerView.NO_POSITION;
    mPendingScrollPositionOffset = INVALID_OFFSET;
    mLastStackFromEnd = mStackFromEnd;
    // we don't need this anymore
    mPendingSavedState = null;
    if (DEBUG) {
        validateChildOrder();
    }
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)106 View (android.view.View)100 TextView (android.widget.TextView)19 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)15 ImageView (android.widget.ImageView)13 OrientationHelper (android.support.v7.widget.OrientationHelper)12 GridLayoutManager (android.support.v7.widget.GridLayoutManager)10 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)10 Point (android.graphics.Point)7 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)7 SuppressLint (android.annotation.SuppressLint)6 Context (android.content.Context)6 Bundle (android.os.Bundle)6 ActivityManager (android.app.ActivityManager)5 AccessibilityNodeInfoCompat (android.support.v4.view.accessibility.AccessibilityNodeInfoCompat)5 SpanSizeLookup (android.support.v7.widget.GridLayoutManager.SpanSizeLookup)5 LayoutParams (android.support.v7.widget.RecyclerView.LayoutParams)5 Recycler (android.support.v7.widget.RecyclerView.Recycler)5 RecyclerListener (android.support.v7.widget.RecyclerView.RecyclerListener)5 ViewGroup (android.view.ViewGroup)5