androidx.recyclerview.widget.RecyclerView#getPaddingTop ( )源码实例Demo

下面列出了androidx.recyclerview.widget.RecyclerView#getPaddingTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: WidgetCase   文件: DividerItemDecoration.java
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();
    final int top;
    final int bottom;
    //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top,
                parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount - 1; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(child.getTranslationX());
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}
 
public void drawHorizontal(Canvas canvas, RecyclerView parent) {
    int top = parent.getPaddingTop();
    int bottom = parent.getHeight() - parent.getPaddingBottom();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        if (!hasDivider(parent, child)) {
            continue;
        }

        if (mDrawable.isStateful()) {
            mDrawable.setState(child.getDrawableState());
        }

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        int left = child.getRight() + params.rightMargin + Math.round(child.getTranslationX());
        int right = left + mDrawable.getIntrinsicWidth();
        drawDivider(canvas, left, top, right, bottom, child.getAlpha());
    }
}
 
源代码3 项目: a   文件: GridSpacingItemDecoration.java
private void drawVertical(Canvas canvas, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
    final int childSize = parent.getChildCount();
    for (int i = 0; i < childSize; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        final int left = child.getRight() + layoutParams.rightMargin;
        final int right = left + space;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }
}
 
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();
    int top;
    int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    int childCount = parent.getChildCount();

    for (int i = 0; i < childCount; ++i) {
        View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        int right = mBounds.right + Math.round(child.getTranslationX());
        int left = right - mDivider.getIntrinsicWidth();
        this.mDivider.setBounds(left, top, right, bottom);
        this.mDivider.draw(canvas);
    }

    canvas.restore();
}
 
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();
    final int top;
    final int bottom;
    //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top,
                parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(child.getTranslationX());
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}
 
源代码6 项目: UltimateRecyclerView   文件: HeaderRenderer.java
/**
 * Gets a clipping rect for the header based on the margins of the header and the padding of the
 * recycler.
 * FIXME: Currently right margin in VERTICAL orientation and bottom margin in HORIZONTAL
 * orientation are clipped so they look accurate, but the headers are not being drawn at the
 * correctly smaller width and height respectively.
 *
 * @param recyclerView for which to provide a header
 * @param header       for clipping
 * @return a {@link Rect} for clipping a provided header to the padding of a recycler view
 */
private Rect getClipRectForHeader(RecyclerView recyclerView, View header) {
  Rect headerMargins = mDimensionCalculator.getMargins(header);
  if (mOrientationProvider.getOrientation(recyclerView) == LinearLayout.VERTICAL) {
    return new Rect(
        recyclerView.getPaddingLeft(),
        recyclerView.getPaddingTop(),
        recyclerView.getWidth() - recyclerView.getPaddingRight() - headerMargins.right,
        recyclerView.getHeight() - recyclerView.getPaddingBottom());
  } else {
    return new Rect(
        recyclerView.getPaddingLeft(),
        recyclerView.getPaddingTop(),
        recyclerView.getWidth() - recyclerView.getPaddingRight(),
        recyclerView.getHeight() - recyclerView.getPaddingBottom() - headerMargins.bottom);
  }
}
 
@SuppressLint("NewApi")
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();
    final int top;
    final int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top,
                parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}
 
源代码8 项目: libcommon   文件: DividerItemDecoration.java
protected void drawHorizontal(final Canvas canvas, final RecyclerView parent) {
	final RecyclerView.LayoutManager manager = parent.getLayoutManager();

	final int top = parent.getPaddingTop();
	final int bottom = parent.getHeight() - parent.getPaddingBottom();

	final int childCount = parent.getChildCount() - 1;
	for (int i = 0; i < childCount; i++) {
		final View child = parent.getChildAt(i);
		if (hasDivider(child)) {
			final int left = child.getLeft();
			final int right = left + mDivider.getIntrinsicWidth();
			mDivider.setBounds(left, top, right, bottom);
			mDivider.draw(canvas);
		}
	}
}
 
源代码9 项目: monero-wallet-android-app   文件: WeSwipeHelper.java
/**
 * Called when {@link #onMove(RecyclerView, RecyclerView.ViewHolder, RecyclerView.ViewHolder)} returns true.
 * <p>
 * WeSwipeHelper does not create an extra Bitmap or View while dragging, instead, it
 * modifies the existing View. Because of this reason, it is important that the View is
 * still part of the layout after it is moved. This may not work as intended when swapped
 * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
 * which were not eligible for dropping over).
 * <p>
 * This method is responsible to give necessary hint to the LayoutManager so that it will
 * keep the View in visible area. For example, for LinearLayoutManager, this is as simple
 * as calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}.
 * <p>
 * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's
 * new position is likely to be out of bounds.
 * <p>
 * It is important to ensure the ViewHolder will stay visible as otherwise, it might be
 * removed by the LayoutManager if the move causes the View to go out of bounds. In that
 * case, drag will end prematurely.
 *
 * @param recyclerView The RecyclerView controlled by the WeSwipeHelper.
 * @param viewHolder   The ViewHolder under user's control.
 * @param fromPos      The previous adapter position of the dragged item (before it was
 *                     moved).
 * @param target       The ViewHolder on which the currently active item has been dropped.
 * @param toPos        The new adapter position of the dragged item.
 * @param x            The updated left value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 * @param y            The updated top value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 */
public void onMoved(final RecyclerView recyclerView,
                    final RecyclerView.ViewHolder viewHolder, int fromPos, final RecyclerView.ViewHolder target, int toPos, int x,
                    int y) {
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof WeSwipeHelper.ViewDropHandler) {
        ((WeSwipeHelper.ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
                target.itemView, x, y);
        return;
    }

    // if layout manager cannot handle it, do some guesswork
    if (layoutManager.canScrollHorizontally()) {
        final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
        if (minLeft <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxRight = layoutManager.getDecoratedRight(target.itemView);
        if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }

    if (layoutManager.canScrollVertically()) {
        final int minTop = layoutManager.getDecoratedTop(target.itemView);
        if (minTop <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
        if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}
 
源代码10 项目: android-test   文件: DividerItemDecoration.java
public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
private void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();

    int top;
    int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    RecyclerView.LayoutManager lm = parent.getLayoutManager();
    if (lm == null) return;

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        if (isDecorated(i)) {
            View child = parent.getChildAt(i);
            lm.getDecoratedBoundsWithMargins(child, mBounds);
            int right = mBounds.right + Math.round(child.getTranslationX());
            int left = right - mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
    }

    canvas.restore();
}
 
源代码13 项目: MyBookshelf   文件: DividerItemDecoration.java
public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDrawable.getIntrinsicHeight();
        mDrawable.setBounds(left, top, right, bottom);
        mDrawable.draw(c);
    }
}
 
源代码14 项目: sa-sdk-android   文件: HorizonRecyclerDivider.java
public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
源代码15 项目: CrazyDaily   文件: ItemTouchHelperExtension.java
/**
 * Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true.
 * <p>
 * ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it
 * modifies the existing View. Because of this reason, it is important that the View is
 * still part of the layout after it is moved. This may not work as intended when swapped
 * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
 * which were not eligible for dropping over).
 * <p>
 * This method is responsible to give necessary hint to the LayoutManager so that it will
 * keep the View in visible area. For example, for LinearLayoutManager, this is as simple
 * <p>
 * <p>
 * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's
 * new position is likely to be out of bounds.
 * <p>
 * It is important to ensure the ViewHolder will stay visible as otherwise, it might be
 * removed by the LayoutManager if the move causes the View to go out of bounds. In that
 * case, drag will end prematurely.
 *
 * @param recyclerView The RecyclerView controlled by the ItemTouchHelper.
 * @param viewHolder   The ViewHolder under user's control.
 * @param fromPos      The previous adapter position of the dragged item (before it was
 *                     moved).
 * @param target       The ViewHolder on which the currently active item has been dropped.
 * @param toPos        The new adapter position of the dragged item.
 * @param x            The updated left value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 * @param y            The updated top value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 */
public void onMoved(final RecyclerView recyclerView,
                    final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x,
                    int y) {
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof ViewDropHandler) {
        ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
                target.itemView, x, y);
        return;
    }

    // if layout manager cannot handle it, do some guesswork
    if (layoutManager.canScrollHorizontally()) {
        final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
        if (minLeft <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxRight = layoutManager.getDecoratedRight(target.itemView);
        if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }

    if (layoutManager.canScrollVertically()) {
        final int minTop = layoutManager.getDecoratedTop(target.itemView);
        if (minTop <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
        if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}
 
源代码16 项目: RvHelper   文件: DividerItemDecoration.java
public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
源代码17 项目: GetApk   文件: StickyHeaderDecoration.java
protected void createPinnedHeader(RecyclerView parent) {
    updateStickyHeader(parent);

    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager == null || layoutManager.getChildCount() <= 0) {
        return;
    }
    int firstVisiblePosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
    int firstCompletelyVisiblePosition = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
    int headerPosition = findStickyHeaderPosition(parent, firstVisiblePosition);
    if (headerPosition == -1 || (headerPosition == firstCompletelyVisiblePosition)) {
        resetPinnedHeader();
        return;
    }
    if (headerPosition >= 0 && mHeaderPosition != headerPosition) {
        mHeaderPosition = headerPosition;
        int viewType = mAdapter.getItemViewType(headerPosition);
        mCurrentItemType = viewType;

        RecyclerView.ViewHolder stickyViewHolder = mAdapter.createViewHolder(parent, viewType);
        if (mAdapter instanceof SwipeStickyAdapter) {
            ((SwipeStickyAdapter) mAdapter).onBindSwipeViewHolder(stickyViewHolder, headerPosition);
        } else {
            mAdapter.onBindViewHolder(stickyViewHolder, headerPosition);
        }

        mStickyView = stickyViewHolder.itemView;


        // read layout parameters
        ViewGroup.LayoutParams layoutParams = mStickyView.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mStickyView.setLayoutParams(layoutParams);
        }

        int heightMode = View.MeasureSpec.getMode(layoutParams.height);
        int heightSize = View.MeasureSpec.getSize(layoutParams.height);

        if (heightMode == View.MeasureSpec.UNSPECIFIED) {
            heightMode = View.MeasureSpec.EXACTLY;
        }

        int maxHeight = parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom();
        if (heightSize > maxHeight) {
            heightSize = maxHeight;
        }

        // measure & layout
        int ws = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.EXACTLY);
        int hs = View.MeasureSpec.makeMeasureSpec(heightSize, heightMode);
        mStickyView.measure(ws, hs);
        mStickyView.layout(0, 0, mStickyView.getMeasuredWidth(), mStickyView.getMeasuredHeight());
    }
}
 
源代码18 项目: CalendarView   文件: GroupItemDecoration.java
/**
 * 绘制悬浮组
 *
 * @param c      Canvas
 * @param parent RecyclerView
 */
protected void onDrawOverGroup(Canvas c, RecyclerView parent) {
    int firstVisiblePosition = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (firstVisiblePosition == RecyclerView.NO_POSITION) {
        return;
    }
    Group group = getCroup(firstVisiblePosition);
    if (group == null)
        return;
    String groupTitle = group.toString();
    if (TextUtils.isEmpty(groupTitle)) {
        return;
    }
    boolean isRestore = false;
    Group nextGroup = getCroup(firstVisiblePosition + 1);
    if (nextGroup != null && !group.equals(nextGroup)) {
        //说明是当前组最后一个元素,但不一定碰撞了
        View child = parent.findViewHolderForAdapterPosition(firstVisiblePosition).itemView;
        if (child.getTop() + child.getMeasuredHeight() < mGroupHeight) {
            //进一步检测碰撞
            c.save();//保存画布当前的状态
            isRestore = true;
            c.translate(0, child.getTop() + child.getMeasuredHeight() - mGroupHeight);
        }
    }
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();
    int top = parent.getPaddingTop();
    int bottom = top + mGroupHeight;
    c.drawRect(left, top, right, bottom, mBackgroundPaint);
    float x;
    float y = top + mTextBaseLine;
    if (isCenter) {
        x = parent.getMeasuredWidth() / 2 - getTextX(groupTitle);
    } else {
        x = mPaddingLeft;
    }
    c.drawText(groupTitle, x, y, mTextPaint);
    if (isRestore) {
        //还原画布为初始状态
        c.restore();
    }
}
 
源代码19 项目: Mysplash   文件: FollowingItemDecoration.java
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                           @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager == null) {
        return;
    }

    FollowingAdapter adapter = (FollowingAdapter) parent.getAdapter();
    if (adapter == null) {
        return;
    }

    parentPaddingLeft = parent.getPaddingLeft();
    parentPaddingRight = parent.getPaddingRight();
    parentPaddingTop = parent.getPaddingTop();
    parentPaddingBottom = parent.getPaddingBottom();
    insets = getWindowInset(parent);
    if (parentPaddingLeft != insets.left
            || parentPaddingRight != insets.right
            || parentPaddingTop != 0
            || parentPaddingBottom != insets.bottom) {
        setParentPadding(parent, insets);
    }

    StaggeredGridLayoutManager.LayoutParams params
            = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

    int spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
    int spanIndex = params.getSpanIndex();

    if (adapter.isPhotoItem(params.getViewAdapterPosition())) {
        if (spanCount == 1) {
            outRect.set(
                    marginSingleSpanPhoto.left,
                    0,
                    marginSingleSpanPhoto.right,
                    marginSingleSpanPhoto.bottom
            );
        } else {
            if (spanIndex == 0) {
                outRect.set(marginGridItem, 0, marginGridItem, marginGridItem);
            } else {
                outRect.set(0, 0, marginGridItem, marginGridItem);
            }
        }
    }
}
 
源代码20 项目: Carbon   文件: ItemTouchHelper.java
/**
 * Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true.
 * <p>
 * ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it
 * modifies the existing View. Because of this reason, it is important that the View is
 * still part of the layout after it is moved. This may not work as intended when swapped
 * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
 * which were not eligible for dropping over).
 * <p>
 * This method is responsible to give necessary hint to the LayoutManager so that it will
 * keep the View in visible area. For example, for LinearLayoutManager, this is as simple as
 * calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}.
 * <p>
 * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's new
 * position is likely to be out of bounds.
 * <p>
 * It is important to ensure the ViewHolder will stay visible as otherwise, it might be
 * removed by the LayoutManager if the move causes the View to go out of bounds. In that
 * case, drag will end prematurely.
 *
 * @param recyclerView The RecyclerView controlled by the ItemTouchHelper.
 * @param viewHolder   The ViewHolder under user's control.
 * @param fromPos      The previous adapter position of the dragged item (before it was
 *                     moved).
 * @param target       The ViewHolder on which the currently active item has been dropped.
 * @param toPos        The new adapter position of the dragged item.
 * @param x            The updated left value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by {@link
 *                     RecyclerView.ItemDecoration}s.
 * @param y            The updated top value of the dragged View after drag translations are
 *                     applied. This value does not include margins added by {@link
 *                     RecyclerView.ItemDecoration}s.
 */
public void onMoved(final RecyclerView recyclerView,
                    final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x,
                    int y) {
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof ViewDropHandler) {
        ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
                target.itemView, x, y);
        return;
    }
    // if layout manager cannot handle it, do some guesswork
    if (layoutManager.canScrollHorizontally()) {
        final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
        if (minLeft <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxRight = layoutManager.getDecoratedRight(target.itemView);
        if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
    if (layoutManager.canScrollVertically()) {
        final int minTop = layoutManager.getDecoratedTop(target.itemView);
        if (minTop <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
        if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}