下面列出了androidx.recyclerview.widget.RecyclerView#getChildAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void drawVertical(Canvas canvas, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final 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 top = child.getBottom() + params.bottomMargin + Math.round(child.getTranslationY());
int bottom = top + mDrawable.getIntrinsicHeight();
drawDivider(canvas, left, top, right, bottom, child.getAlpha());
}
}
/**
* {@inheritDoc}
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
final int count = parent.getChildCount();
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
final View child = parent.getChildAt(translatedChildPosition(parent, layoutPos));
final int adapterPos = parent.getChildAdapterPosition(child);
if (adapterPos != RecyclerView.NO_POSITION && ((layoutPos == 0 && sticky) || hasHeader(parent, adapter, adapterPos))) {
View header = getHeader(parent, adapter, adapterPos).itemView;
c.save();
final int left = child.getLeft();
final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
c.translate(left, top);
header.draw(c);
c.restore();
}
}
}
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int childCount = parent.getChildCount();
final boolean isRTL =
ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL;
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
if (isChildInLastColumn(parent, child)) {
continue;
}
parent.getDecoratedBoundsWithMargins(child, bounds);
final int x = isRTL ? bounds.left : bounds.right;
final int startY = bounds.top;
final int stopY = bounds.bottom;
canvas.drawLine(x, startY, x, stopY, dividerPaint);
}
}
@Override
public void onDrawOver(final Canvas c, final RecyclerView parent,
final RecyclerView.State state) {
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 + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (divider != null) {
int count = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
View child = null;
for (int i = 0; i < count; i++) {
if (count == 1) {
break;
}
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(c);
}
}
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int width = parent.getWidth();
int top;
int childCount = parent.getChildCount() - (single ? 0 : 1);
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
View nextChild = i < childCount - 1 ? parent.getChildAt(i + 1) : null;
int position = parent.getChildAdapterPosition(child);
if (position < skipRows || child instanceof GroupCreateSectionCell || nextChild instanceof GroupCreateSectionCell) {
continue;
}
top = child.getBottom();
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(72), top, width - (LocaleController.isRTL ? AndroidUtilities.dp(72) : 0), top, Theme.dividerPaint);
}
}
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
+ mWidthDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mWidthDivider.getIntrinsicHeight();
mWidthDivider.setBounds(left, top, right, bottom);
mWidthDivider.draw(c);
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final int left = child.getLeft();
final int right = child.getRight();
final int top = child.getTop();
final int bottom = child.getBottom();
c.drawRect(left + devideWidth / 2, top + devideWidth / 2, right - devideWidth / 2, bottom - devideWidth / 2, maint);
}
}
protected int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos,
int layoutPos) {
int headerHeight = getHeaderHeightForLayout(header);
int top = getChildY(parent, child) - headerHeight;
if (sticky && layoutPos == 0) {
final int count = parent.getChildCount();
final long currentId = adapter.getHeaderId(adapterPos);
// find next view with header and compute the offscreen push if needed
for (int i = 1; i < count; i++) {
int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(translatedChildPosition(parent, i)));
if (adapterPosHere != RecyclerView.NO_POSITION) {
long nextId = adapter.getHeaderId(adapterPosHere);
if (nextId != currentId) {
final View next = parent.getChildAt(translatedChildPosition(parent, i));
final int offset = getChildY(parent, next) - (headerHeight + getHeader(parent, adapter, adapterPosHere).itemView.getHeight());
if (offset < 0) {
return offset;
} else {
break;
}
}
}
}
if (sticky) {
top = Math.max(0, top);
}
}
return top;
}
private void drawVertical(Canvas canvas, RecyclerView parent) {
canvas.save();
int left;
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();
}
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
if (isDecorated(i)) {
View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);
int bottom = mBounds.bottom + Math.round(child.getTranslationY());
int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
}
canvas.restore();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + dividerHeight;
c.drawRect(left, top, right, bottom, dividerPaint);
}
}
private int getSubHeaderTop(@NonNull RecyclerView parent, @NonNull View child,
@NonNull View header, @NonNull View subHeader, int adapterPos, int layoutPos) {
int top = getAnimatedTop(child) - getSubHeaderHeightForLayout(subHeader);
if (isFirstValidChild(layoutPos, parent)) {
final int count = parent.getChildCount();
final long currentHeaderId = adapter.getHeaderId(adapterPos);
final long currentSubHeaderId = adapter.getSubHeaderId(adapterPos);
// find next view with sub-header and compute the offscreen push if needed
for (int i = layoutPos + 1; i < count; i++) {
final View next = parent.getChildAt(i);
int adapterPosHere = parent.getChildAdapterPosition(next);
if (adapterPosHere != RecyclerView.NO_POSITION) {
final long nextHeaderId = adapter.getHeaderId(adapterPosHere);
final long nextSubHeaderId = adapter.getSubHeaderId(adapterPosHere);
if ((nextSubHeaderId != currentSubHeaderId)) {
int headersHeight = getSubHeaderHeightForLayout(subHeader) +
getSubHeader(parent, adapterPosHere).itemView.getHeight();
if (nextHeaderId != currentHeaderId) {
headersHeight += getHeader(parent, adapterPosHere).itemView.getHeight();
}
final int offset = getAnimatedTop(next) - headersHeight;
if (offset < header.getHeight()) {
return offset;
} else {
break;
}
}
}
}
}
return Math.max(header.getHeight(), top);
}
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();
RecyclerView.Adapter adapter = parent.getAdapter();
final int itemCount = adapter == null ? -1 : adapter.getItemCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
int adapterPosition = parent.getChildAdapterPosition(child);
if (adapterPosition == 0 || adapterPosition == itemCount - 1)
continue;
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
canvas.restore();
}
/**
* 遍历所有 {@link BaseHolder}, 释放他们需要释放的资源
*
* @param recyclerView {@link RecyclerView}
*/
public static void releaseAllHolder(RecyclerView recyclerView) {
if (recyclerView == null) {
return;
}
for (int i = recyclerView.getChildCount() - 1; i >= 0; i--) {
final View view = recyclerView.getChildAt(i);
RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);
if (viewHolder instanceof BaseHolder) {
((BaseHolder) viewHolder).onRelease();
}
}
}
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();
}
@Override public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
final int left = /* parent.getPaddingLeft() + */ customHorizontalPadding;
final int right = parent.getWidth() /* - parent.getPaddingRight() */ - customHorizontalPadding;
final int childCount = parent.getChildCount();
for (int i = 0; i < (childCount - 1); 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 + dividerDrawable.getIntrinsicHeight();
dividerDrawable.setBounds(left, top, right, bottom);
dividerDrawable.draw(canvas);
}
}
/**
* Draw a horizontal line.
*
* @param canvas Canvas
* @param parent RecyclerView
*/
private void drawHorizontalLine(Canvas canvas, RecyclerView parent) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager == null || mDivider == null) {
return;
}
canvas.save();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
if (isLastRow(child, parent)) {
continue;
}
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int left = mBounds.left;
final int right = mBounds.right;
int bottom = mBounds.bottom + Math.round(child.getTranslationY());
// Horizontal GridLayout display in the left, margin top or bottom mDividerHeight / 2.
if (mOrientation == HORIZONTAL && layoutManager instanceof GridLayoutManager) {
bottom += mDividerHeight / 2;
}
final int top = bottom - mDividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
canvas.restore();
}
private 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);
}
}
private void updateSections(RecyclerView listView, boolean checkTopBottom) {
int count = listView.getChildCount();
int minPositionDateHolder = Integer.MAX_VALUE;
View minDateChild = null;
float padding = listView.getPaddingTop() + actionBar.getTranslationY();
int minTop = Integer.MAX_VALUE;
int maxBottom = 0;
for (int a = 0; a < count; a++) {
View view = listView.getChildAt(a);
int bottom = view.getBottom();
minTop = Math.min(minTop, view.getTop());
maxBottom = Math.max(bottom, maxBottom);
if (bottom <= padding) {
continue;
}
int position = view.getBottom();
if (view instanceof SharedMediaSectionCell || view instanceof GraySectionCell) {
if (view.getAlpha() != 1.0f) {
view.setAlpha(1.0f);
}
if (position < minPositionDateHolder) {
minPositionDateHolder = position;
minDateChild = view;
}
}
}
if (minDateChild != null) {
if (minDateChild.getTop() > padding) {
if (minDateChild.getAlpha() != 1.0f) {
minDateChild.setAlpha(1.0f);
}
} else {
if (minDateChild.getAlpha() != 0.0f) {
minDateChild.setAlpha(0.0f);
}
}
}
if (checkTopBottom) {
if (maxBottom != 0 && maxBottom < (listView.getMeasuredHeight() - listView.getPaddingBottom())) {
resetScroll();
} else if (minTop != Integer.MAX_VALUE && minTop > listView.getPaddingTop() + actionBar.getTranslationY()) {
scrollWithoutActionBar(listView, -listView.computeVerticalScrollOffset());
resetScroll();
}
}
}
/***/
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);
}
}
}
}