下面列出了androidx.recyclerview.widget.RecyclerView#getPaddingRight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
void scrollToPosition(RecyclerView list, int pos) {
if (list.getLayoutManager() instanceof LinearLayoutManager) {
// Centering item in its parent
final LinearLayoutManager manager = (LinearLayoutManager) list.getLayoutManager();
final boolean isHorizontal = manager.getOrientation() == LinearLayoutManager.HORIZONTAL;
int offset = isHorizontal
? (list.getWidth() - list.getPaddingLeft() - list.getPaddingRight()) / 2
: (list.getHeight() - list.getPaddingTop() - list.getPaddingBottom()) / 2;
final RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
if (holder != null) {
final View view = holder.itemView;
offset -= isHorizontal ? view.getWidth() / 2 : view.getHeight() / 2;
}
manager.scrollToPositionWithOffset(pos, offset);
} else {
list.scrollToPosition(pos);
}
}
/**
* Performs the actual calculation for determining the number of possible
* columns by using the {@link #maxColumnCount}, {@link #minColumnSpacingEdge}, and
* {@link #minColumnSpacingSeparator} in conjunction with the requested width
* for the items
*
* @param recyclerView The RecyclerView to use when determining the possible number of columns
* @param gridItemWidth The requested width for items to be
* @return The calculated number of possible columns
*/
protected int getColumnCount(@NonNull RecyclerView recyclerView, int gridItemWidth) {
int padding = recyclerView.getPaddingLeft() + recyclerView.getPaddingRight();
int usableWidth = recyclerView.getWidth() - padding;
int columnCount = Math.min(usableWidth / gridItemWidth, maxColumnCount);
int usedColumnWidth, minRequiredSpacing;
//Decreases the columnCount until the specified min spacing can be achieved.
do {
usedColumnWidth = columnCount * gridItemWidth;
minRequiredSpacing = (2 * minColumnSpacingEdge) + ((columnCount -1) * minColumnSpacingSeparator);
//If the specified min spacing is reached, return the number of columns
if (usableWidth - usedColumnWidth - minRequiredSpacing >= 0) {
return columnCount;
}
columnCount--;
} while(columnCount > 1);
return columnCount;
}
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getMeasuredWidth() - parent.getPaddingRight();
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();
int top = child.getBottom() + layoutParams.bottomMargin;
int bottom = top + space;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
/**
* 绘制分组Group
*
* @param c Canvas
* @param parent RecyclerView
*/
protected void onDrawGroup(Canvas c, RecyclerView parent) {
int paddingLeft = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int top, bottom;
int count = parent.getChildCount();
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int key = params.getViewLayoutPosition();
if (mGroup.containsKey(key)) {
top = child.getTop() - params.topMargin - mGroupHeight;
bottom = top + mGroupHeight;
c.drawRect(paddingLeft, top, right, bottom, mBackgroundPaint);
String group = mGroup.get(params.getViewLayoutPosition()).toString();
float x;
float y = top + mTextBaseLine;
if (isCenter) {
x = parent.getMeasuredWidth() / 2 - getTextX(group);
} else {
x = mPaddingLeft;
}
c.drawText(group, x, y, mTextPaint);
}
}
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
canvas.save();
final int right;
//noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
if (parent.getClipToPadding()) {
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(leftPadding, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
} else {
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, bounds);
final int bottom = bounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - 1;
divider.setBounds(leftPadding, top, right, bottom);
divider.draw(canvas);
}
canvas.restore();
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i <= childCount - 2; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();
mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
mDivider.draw(canvas);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
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);
}
}
private 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 + mDividerSize;
if (mDividerDrawable != null)
{
mDividerDrawable.setBounds(left, top, right, bottom);
mDividerDrawable.draw(c);
} else if (mDividerPaint != null)
{
c.drawRect(left, top, right, bottom, mDividerPaint);
}
}
}
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(canvas);
}
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
canvas.save();
final int left;
final int right;
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(left, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0;
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
if (!hasDivider(parent, child))
continue;
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.setAlpha((int) (child.getAlpha() * 255));
mDivider.draw(canvas);
}
canvas.restore();
}
private void drawVertical(Canvas canvas, RecyclerView parent) {
canvas.save();
final int left;
final int right;
//noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(left, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0;
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - mDividerSize;
mDividerPaint.setColor(mDividerColor);
mDividerPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(left + mMarginStart, top + mMarginTop, right - mMarginEnd, bottom - mMarginBottom, mDividerPaint);
}
canvas.restore();
}
private 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);
}
}
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);
}
}
public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++) {
final View child = parent.getChildAt(i);
//获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left + SIZE_PADDING, top, right - SIZE_PADDING, bottom);
mDivider.draw(c);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin + (int)child.getTranslationY();
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (parent.getLayoutManager() == null) {
return;
}
c.save();
final int left;
final int right;
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
c.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0;
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, bounds);
int position = parent.getChildAdapterPosition(child);
if (parent.getAdapter().getItemViewType(position) == EventListAdapter.ITEM_TYPE_HEADER ||
parent.getAdapter().getItemViewType(position + 1) == EventListAdapter.ITEM_TYPE_HEADER ||
position == state.getItemCount() - 1) {
continue;
}
final int bottom = bounds.bottom + Math.round(ViewCompat.getTranslationY(child));
final int top = bottom - divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
c.restore();
}
private void drawVertical(Canvas c, RecyclerView parent) {
int firstDataItemPosition = 0;
int lastDataItemPosition = 0;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
AssemblyRecyclerAdapter recyclerAdapter = null;
if (adapter instanceof AssemblyRecyclerAdapter) {
recyclerAdapter = (AssemblyRecyclerAdapter) adapter;
firstDataItemPosition = recyclerAdapter.getHeaderCount();
lastDataItemPosition = firstDataItemPosition + (recyclerAdapter.getDataCount() - 1);
}
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);
int itemPosition = ((RecyclerView.LayoutParams) child.getLayoutParams()).getViewLayoutPosition();
int bottomMargin = ((RecyclerView.LayoutParams) child.getLayoutParams()).bottomMargin;
if (recyclerAdapter == null || (itemPosition >= firstDataItemPosition && itemPosition <= lastDataItemPosition)) {
final int top = child.getBottom() + bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
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);
}
}
/**
* Calculates and adds the amount of spacing that needs to be between each
* column, row, and the edges of the RecyclerView. This pays attention to
* the value from {@link #setSpacingMethod(SpacingMethod)}
*
* @param recyclerView The RecyclerView to use for determining the amount of space that needs to be added
* @param gridItemWidth The requested width for the items
* @param columnCount The number of columns to display
*/
protected void updateSpacing(@NonNull RecyclerView recyclerView, int gridItemWidth, int columnCount) {
//Sets the decoration for the calculated spacing
if (spacerDecoration == null) {
spacerDecoration = new SpacerDecoration();
spacerDecoration.setAllowedEdgeSpacing(SpacerDecoration.EDGE_SPACING_LEFT | SpacerDecoration.EDGE_SPACING_RIGHT);
recyclerView.addItemDecoration(spacerDecoration);
}
edgeSpacing = minColumnSpacingEdge;
int separatorSpacing = minColumnSpacingSeparator / 2;
//Calculates the edge spacing requirements
int padding = recyclerView.getPaddingLeft() + recyclerView.getPaddingRight();
int usableWidth = recyclerView.getWidth() - padding;
int separatorCount = columnCount -1;
int spacerCount = 2 * columnCount;
int freeSpace = usableWidth - (gridItemWidth * columnCount);
int extraSpace = freeSpace - (2 * minColumnSpacingEdge) - (separatorCount * minColumnSpacingSeparator);
//If we can add spacing, then we need to calculate how much and where to add it
if (extraSpace >= spacerCount) {
if (spacingMethod == SpacingMethod.ALL) {
int totalMinEdges = minColumnSpacingEdge * 2;
int totalMinSeparators = separatorCount * minColumnSpacingSeparator;
//If the totalMinSpace is 0, then the percentage is edge count / separators + edges
int totalMinSpace = totalMinEdges + totalMinSeparators;
double edgeSpacePercentage = totalMinSpace == 0 ? (2 / (2 + separatorCount)) : (double)totalMinEdges / (double)totalMinSpace;
int totalSeparatorSpace = (int)((1d - edgeSpacePercentage) * freeSpace);
separatorSpacing = spacerCount == 0 ? 0 : totalSeparatorSpace / spacerCount;
edgeSpacing = ((freeSpace - totalSeparatorSpace) / 2) + separatorSpacing;
} else if (spacingMethod == SpacingMethod.EDGES) {
edgeSpacing = (freeSpace - (separatorSpacing * spacerCount)) / 2;
} else { //SEPARATOR
separatorSpacing = spacerCount == 0 ? 0 : freeSpace / spacerCount;
}
edgeSpacing -= separatorSpacing;
}
//Updates the spacing using the decoration and padding
recyclerView.setPadding(
recyclerView.getPaddingLeft() + edgeSpacing,
recyclerView.getPaddingTop(),
recyclerView.getPaddingRight() + edgeSpacing,
recyclerView.getPaddingBottom()
);
spacerDecoration.update(separatorSpacing, matchSpacing ? separatorSpacing : rowSpacing / 2);
}
/**
* 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);
}
}
}