下面列出了androidx.recyclerview.widget.RecyclerView#getChildCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onDraw(@NotNull Canvas canvas, @NotNull RecyclerView parent, @NotNull 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);
}
}
@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 GraySectionCell || nextChild instanceof GraySectionCell) {
continue;
}
top = child.getBottom();
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(72), top, width - (LocaleController.isRTL ? AndroidUtilities.dp(72) : 0), top, Theme.dividerPaint);
}
}
private void drawVertical(Canvas canvas, RecyclerView parent) {
canvas.save();
// Drawable width defines left/right padding. Drawable height defines divider height.
final int left = parent.getPaddingStart() + divider.getIntrinsicWidth();
final int right = parent.getWidth() - parent.getPaddingEnd() - divider.getIntrinsicWidth();
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 - divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(canvas);
}
canvas.restore();
}
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 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);
}
}
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 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) {
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);
}
}
}
@Override
public void onBindDragView(View clickedView, View dragView) {
LinearLayout clickedLayout = (LinearLayout) clickedView;
View clickedHeader = clickedLayout.getChildAt(0);
RecyclerView clickedRecyclerView = (RecyclerView) clickedLayout.getChildAt(1);
View dragHeader = dragView.findViewById(R.id.drag_header);
ScrollView dragScrollView = dragView.findViewById(R.id.drag_scroll_view);
LinearLayout dragLayout = dragView.findViewById(R.id.drag_list);
dragLayout.removeAllViews();
((TextView) dragHeader.findViewById(R.id.text)).setText(((TextView) clickedHeader.findViewById(R.id.text)).getText());
((TextView) dragHeader.findViewById(R.id.item_count)).setText(((TextView) clickedHeader.findViewById(R.id.item_count)).getText());
for (int i = 0; i < clickedRecyclerView.getChildCount(); i++) {
View mapItemView = View.inflate(dragView.getContext(), R.layout.column_item, null);
((TextView) mapItemView.findViewById(R.id.text)).setText(((TextView) clickedRecyclerView.getChildAt(i).findViewById(R.id.text)).getText());
dragLayout.addView(mapItemView);
if (i == 0) {
dragScrollView.setScrollY(-clickedRecyclerView.getChildAt(i).getTop());
}
}
dragView.setPivotY(0);
dragView.setPivotX(clickedView.getMeasuredWidth() / 2);
}
@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);
}
}
@SuppressLint("NewApi")
protected void drawVertical(Canvas canvas, RecyclerView parent) {
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 itemCount = parent.getChildCount();
for (int i = 0; i < itemCount - mDividerOnLastItem; i++) {
final View child = parent.getChildAt(i);
RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);
if (shouldDrawDivider(viewHolder)) {
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();
}
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);
}
/**
* {@inheritDoc}
*/
@Override
public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
final int count = parent.getChildCount();
long previousHeaderId = -1;
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
final View child = parent.getChildAt(layoutPos);
final int adapterPos = parent.getChildAdapterPosition(child);
if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
long headerId = adapter.getHeaderId(adapterPos);
if (headerId != previousHeaderId) {
previousHeaderId = headerId;
View header = getHeader(parent, adapterPos).itemView;
canvas.save();
final int left = child.getLeft();
final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
canvas.translate(left, top);
header.setTranslationX(left);
header.setTranslationY(top);
header.draw(canvas);
canvas.restore();
}
}
}
}
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);
}
}
@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);
}
}
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;
}
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);
}
}
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 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);
}
}
}
}