下面列出了androidx.recyclerview.widget.RecyclerView#getMeasuredWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
}
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 getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
measureItemView(view, parent);
int viewWidth = view.getMeasuredWidth();
if (spanCount * viewWidth < parent.getMeasuredWidth()) {
int emptySpacePx = parent.getMeasuredWidth() - spanCount * viewWidth;
outRect.left = outRect.right = emptySpacePx / (2 * spanCount);
}
}
@Override
public void getItemOffsets(@NonNull final Rect outRect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.right = this.width > 0 ? this.spacing / 2 : this.spacing;
outRect.left = this.width > 0 ? this.spacing / 2 : 0;
if ((state.getItemCount() - 1 == parent.getChildLayoutPosition(view))) {
outRect.right = this.width > 0 ? ((parent.getMeasuredWidth() / 2) - (this.width / 2)) : 0;
}
if (parent.getChildLayoutPosition(view) == 0) {
outRect.left = this.width > 0 ? ((parent.getMeasuredWidth() / 2) - (this.width / 2)) : 0;
}
}
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);
}
}
}
@Override
public boolean isItemsFitOnScreen(RecyclerView recyclerView, int itemsSize) {
calcItemWidth(recyclerView);
int itemsWidth = itemWidth * (itemsSize);
int containerWidth = recyclerView.getMeasuredWidth();
return containerWidth >= itemsWidth;
}
/**
* 绘制悬浮组
*
* @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();
}
}