Java Code Examples for androidx.recyclerview.widget.RecyclerView#LayoutParams

The following examples show how to use androidx.recyclerview.widget.RecyclerView#LayoutParams . 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: GridPaddingItemDecorationBuilder.java    From recyclerview-adapters with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, final RecyclerView.State state) {
    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

    if (!(params instanceof GridLayoutManager.LayoutParams)) return;

    final GridParams gridParams = GridParams.create(view);
    if (gridParams == null) return;

    final int position = gridParams.getPosition();
    final int spanSize = gridParams.getSpanSize();
    final int index = gridParams.getSpanIndex();

    if (spanSize < 1 || index < 0) return;
    if (!usePadding(position)) return;

    boolean useVerticalPadding = paddingType.equals(PaddingType.BOTH) || paddingType.equals(PaddingType.VERTICAL);

    outRect.top = useVerticalPadding ? padding / 2 : 0;
    outRect.bottom = useVerticalPadding ? padding / 2 : 0;
    outRect.left = getPaddingLeft(index, spanSize);
    outRect.right = getPaddingRight(index, spanSize);
}
 
Example 2
Source File: DividerItemDecoration.java    From RecyclerViewExtensions with MIT License 6 votes vote down vote up
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());
    }
}
 
Example 3
Source File: ItemDecorationNotLast.java    From intra42 with Apache License 2.0 6 votes vote down vote up
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getAdapter().getItemCount();
    for (int i = 0; i < childCount; i++) {

        if (i == (childCount - 1)) {
            continue;
        }

        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 4
Source File: DividerGridItemDecoration.java    From cv4j with Apache License 2.0 6 votes vote down vote up
public void drawHorizontal(Canvas c, RecyclerView parent)
{
    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.getLeft() - params.leftMargin;
        final int right = child.getRight() + params.rightMargin
                + mDivider.getIntrinsicWidth();
        final int top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 5
Source File: ColorItemDecoration.java    From FastAdapter with MIT License 5 votes vote down vote up
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
  final int left = parent.getPaddingLeft();
  final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
  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 top = child.getBottom() + layoutParams.bottomMargin;
    final int bottom = top + mDividerHeight;
    if (mPaint != null) {
      canvas.drawRect(left + mMarginLeft, top, right, bottom, mPaint);
    }
  }
}
 
Example 6
Source File: DividerGridItemDecoration.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
public void drawVertical(Canvas c, RecyclerView parent) {
    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 top = child.getTop() - params.topMargin;
        final int bottom = child.getBottom() + params.bottomMargin;
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicWidth();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 7
Source File: TrendHorizontalLinearLayoutManager.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    if (fillCount > 0) {
        int minWidth = (int) DisplayUtils.dpToPx(context, MIN_ITEM_WIDTH);
        int minHeight = (int) DisplayUtils.dpToPx(context, MIN_ITEM_HEIGHT);
        return new RecyclerView.LayoutParams(
                Math.max(minWidth, getWidth() / fillCount),
                getHeight() > minHeight ? ViewGroup.LayoutParams.MATCH_PARENT : minHeight
        );
    } else {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
 
Example 8
Source File: TableViewUtils.java    From TableView with MIT License 5 votes vote down vote up
/**
 * Helps to force width value before calling requestLayout by the system.
 */
public static void setWidth(@NonNull View view, int width) {
    // Change width value from params
    ((RecyclerView.LayoutParams) view.getLayoutParams()).width = width;

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View
            .MeasureSpec.EXACTLY);
    view.measure(widthMeasureSpec, heightMeasureSpec);

    view.requestLayout();
}
 
Example 9
Source File: VerticalDividerItemDecoration.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
protected Rect getDividerBound(int position, RecyclerView parent, View child) {
    Rect bounds = new Rect(0, 0, 0, 0);
    int transitionX = (int) ViewCompat.getTranslationX(child);
    int transitionY = (int) ViewCompat.getTranslationY(child);
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
    bounds.top = child.getTop() + transitionY;
    bounds.bottom = child.getBottom() + transitionY;

    int dividerSize = getDividerSize(position, parent);
    if (mDividerType == DividerType.DRAWABLE || mDividerType == DividerType.SPACE) {
        if (alignTopEdge(parent, position)) {
            bounds.top += mMarginProvider.dividerTopMargin(position, parent);
        }
        if (alignBottomEdge(parent, position)) {
            bounds.bottom -= mMarginProvider.dividerBottomMargin(position, parent);
        }

        bounds.left = child.getRight() + params.rightMargin + transitionX;
        bounds.right = bounds.left + dividerSize;
    } else {
        // set center point of divider
        int halfSize = dividerSize / 2;
        bounds.left = child.getRight() + params.rightMargin + halfSize + transitionX;
        bounds.right = bounds.left;
    }

    if (mPositionInsideItem) {
        bounds.left -= dividerSize;
        bounds.right -= dividerSize;
    }

    return bounds;
}
 
Example 10
Source File: HorizontalDividerItemDecoration.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
protected Rect getDividerBound(int position, RecyclerView parent, View child) {
    Rect bounds = new Rect(0, 0, 0, 0);
    int transitionX = (int) ViewCompat.getTranslationX(child);
    int transitionY = (int) ViewCompat.getTranslationY(child);
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
    bounds.left = child.getLeft() + transitionX;
    bounds.right = child.getRight() + transitionX;

    int dividerSize = getDividerSize(position, parent);
    if (mDividerType == DividerType.DRAWABLE || mDividerType == DividerType.SPACE) {
        if (alignLeftEdge(parent, position)) {
            bounds.left += mMarginProvider.dividerLeftMargin(position, parent);
        }

        if (alignRightEdge(parent, position)) {
            bounds.right -= mMarginProvider.dividerRightMargin(position, parent);
        } else {
            // 交叉位置特殊处理
            bounds.right += getDividerSize(position, parent);
        }
        bounds.top = child.getBottom() + params.bottomMargin + transitionY;
        bounds.bottom = bounds.top + dividerSize;
    } else {
        int halfSize = dividerSize / 2;
        bounds.top = child.getBottom() + params.bottomMargin + halfSize + transitionY;
        bounds.bottom = bounds.top;
    }

    if (mPositionInsideItem) {
        bounds.top -= dividerSize;
        bounds.bottom -= dividerSize;
    }

    return bounds;
}
 
Example 11
Source File: DividerItemDecoration.java    From FChat with MIT License 5 votes vote down vote up
public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
 
    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 top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 12
Source File: DividerItemDecoration.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 13
Source File: DividerItemDecoration.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 + divider.getIntrinsicHeight();
        divider.setBounds(left, top, right, bottom);
        divider.draw(c);
    }
}
 
Example 14
Source File: CardSlideView.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    final Context context = parent.getContext();
    final View view = mHolder.onCreateView(LayoutInflater.from(context), parent);
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
    if (mOrientation == LinearLayout.HORIZONTAL) {
        params.width = Math.round(parent.getMeasuredWidth() / (mSideOffsetPercent * 2 + 1));
        params.height = Math.round(params.width * mItemRate);
    } else {
        params.height = Math.round(parent.getMeasuredHeight() / (mSideOffsetPercent * 2 + 1));
        params.width = Math.round(params.height / mItemRate);
    }
    return new CardViewHolder(view);
}
 
Example 15
Source File: GridSpacingItemDecoration.java    From a with GNU General Public License v3.0 4 votes vote down vote up
/***/
private void drawGrideview1(Canvas canvas, RecyclerView parent) {
    GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
    int childSize = parent.getChildCount();
    int top, bottom, left, right, spancount;
    spancount = linearLayoutManager.getSpanCount();
    for (int i = 0; i < childSize; i++) {
        final View child = parent.getChildAt(i);
        //画横线
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getBottom() + layoutParams.bottomMargin;
        bottom = top + space;
        left = layoutParams.leftMargin + child.getPaddingLeft() + space;
        right = child.getMeasuredWidth() * (i + 1) + left + space * i;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
        //画竖线
        top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);
        bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;
        left = child.getRight() + layoutParams.rightMargin;
        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);
        }

        //画上缺失的线框
        if (i < spancount) {
            top = child.getTop() + layoutParams.topMargin;
            bottom = top + space;
            left = (layoutParams.leftMargin + space) * (i + 1);
            right = child.getMeasuredWidth() * (i + 1) + left + space * i;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
        if (i % spancount == 0) {
            top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);
            bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;
            left = child.getLeft() + layoutParams.leftMargin;
            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);
            }
        }
    }
}
 
Example 16
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 4 votes vote down vote up
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
    return new LayoutParams(c, attrs);
}
 
Example 17
Source File: ExpandableLayoutManager.java    From OmegaRecyclerView with MIT License 4 votes vote down vote up
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
 
Example 18
Source File: TrendHorizontalLinearLayoutManager.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
    return generateDefaultLayoutParams();
}
 
Example 19
Source File: FillLastGridLayoutManager.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void calcLastItemHeight() {
    if (listHeight <= 0 || !shouldCalcLastItemHeight()) {
        return;
    }
    RecyclerView.Adapter adapter = listView.getAdapter();
    if (adapter == null) {
        return;
    }
    int spanCount = getSpanCount();
    int spanCounter = 0;
    int count = adapter.getItemCount() - 1;
    int allHeight = 0;
    final SpanSizeLookup spanSizeLookup = getSpanSizeLookup();
    for (int a = 0; a < count; a++) {
        final int spanSize = spanSizeLookup.getSpanSize(a);
        spanCounter += spanSize;

        if (spanSize == spanCount || spanCounter > spanCount) {
            spanCounter = 0;
        } else if (spanCounter != 1) {
            continue;
        }

        int type = adapter.getItemViewType(a);
        RecyclerView.ViewHolder holder = heights.get(type, null);
        if (holder == null) {
            holder = adapter.createViewHolder(listView, type);
            heights.put(type, holder);
            if (holder.itemView.getLayoutParams() == null) {
                holder.itemView.setLayoutParams(generateDefaultLayoutParams());
            }
        }
        adapter.onBindViewHolder(holder, a);

        final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        final int widthSpec = getChildMeasureSpec(listWidth, getWidthMode(), getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, lp.width, canScrollHorizontally());
        final int heightSpec = getChildMeasureSpec(listHeight, getHeightMode(), getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height, canScrollVertically());
        holder.itemView.measure(widthSpec, heightSpec);
        allHeight += holder.itemView.getMeasuredHeight();
        if (allHeight >= (listHeight - additionalHeight - listView.getPaddingBottom())) {
            break;
        }
    }
    lastItemHeight = Math.max(0, listHeight - allHeight - additionalHeight - listView.getPaddingBottom());
}
 
Example 20
Source File: CommonUtils.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public static void setRecyclerViewTopMargin(@NonNull RecyclerView.ViewHolder holder) {
    if (holder.itemView.getLayoutParams() == null) return;

    Context context = holder.itemView.getContext();
    ((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).topMargin = holder.getLayoutPosition() == 0 ? (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources().getDisplayMetrics()) : 0;
}