如果我将RecycleView
与垂直GridLayoutManager
或StaggeredGridLayoutManager
一起使用,当RecycleView的高度未在屏幕上时,装饰将显示在最后一行下方(这不是我想要的)
我认为getItemOffset()
方法可能有问题吗?
这是装饰代码:
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
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 + dividerSize;
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + dividerSize;
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
private 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 + dividerSize;
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect,
View view,
RecyclerView parent,
RecyclerView.State state) {
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
int itemPosition = parent.getChildAdapterPosition(view);
if (isLastRaw(parent, itemPosition, spanCount, childCount) &&
isLastColum(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, 0, 0);
} else if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, dividerSize, 0);
} else if (isLastColum(parent, itemPosition, spanCount, childCount)) {
outRect.set(0, 0, 0, dividerSize);
} else {
outRect.set(0, 0, dividerSize, dividerSize);
}
}
private int getSpanCount(RecyclerView parent) {
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return spanCount;
}
private boolean isLastColum(RecyclerView parent, int position, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((position + 1) % spanCount == 0) {
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((position + 1) % spanCount == 0) {
return true;
}
} else {
int columCount = childCount / spanCount;
if (childCount % columCount == 0) {
if (position + 1 > childCount - spanCount) {
return true;
}
} else {
if (position + 1 > childCount - childCount % columCount) {
return true;
}
}
}
}
return false;
}
private boolean isLastRaw(RecyclerView parent, int position, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
int rawCount = childCount % spanCount == 0 ?
childCount / spanCount :
childCount / spanCount + 1;
int lastRawFirstPosition = rawCount * spanCount - spanCount + 1;
if (position + 1 >= lastRawFirstPosition) {
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
int rawCount = childCount % spanCount == 0 ?
childCount / spanCount :
childCount / spanCount + 1;
int lastRawFirstPosition = rawCount * spanCount - spanCount + 1;
if (position + 1 >= lastRawFirstPosition) {
return true;
}
} else {
if ((position + 1) % spanCount == 0) {
return true;
}
}
}
return false;
}
当RecycleView的高度超过屏幕时,RecycleView的底部:
当RecycleView的高度未超过屏幕时,RecycleView的底部: