下面列出了androidx.recyclerview.widget.RecyclerView#State ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) < mColumns) {
if (mHorizontal) {
if (mView.getMeasuredWidth() <= 0) {
mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
}
outRect.set(mView.getMeasuredWidth(), 0, 0, 0);
} else {
if (mView.getMeasuredHeight() <= 0) {
mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
}
outRect.set(0, mView.getMeasuredHeight(), 0, 0);
}
} else {
outRect.setEmpty();
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
int itemCount = parent.getChildCount();
for (int i = 0; i < itemCount; i++) {
View item = parent.getChildAt(i);
RecyclerView.LayoutParams itemLayoutParams = (RecyclerView.LayoutParams) item.getLayoutParams();
int left = item.getLeft() - itemLayoutParams.leftMargin;
int right = item.getRight() + itemLayoutParams.rightMargin;
int top = item.getBottom() + itemLayoutParams.bottomMargin;
int bottom = top + dividerSize;
divider.setBounds(left, top, right, bottom);
divider.draw(c);
if(i == 0) {
bottom = item.getTop() + itemLayoutParams.topMargin;
top = bottom - dividerSize;
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
@Override
public void getItemOffsets(@NonNull final Rect outRect, @NonNull final View view,
@NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) {
final int position = parent.getChildAdapterPosition(view);
if (mDivider == null) {
outRect.set(0, 0, 0, 0);
} else {
if (hasDivider(view)) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
} else {
outRect.set(0, 0, 0, 0);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % mSpanCount; // item column
if (mIncludeEdge) {
// spacing - column * ((1f / spanCount) * spacing)
outRect.left = mSpacing - column * mSpacing / mSpanCount;
// (column + 1) * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * mSpacing / mSpanCount;
if (position < mSpanCount) { // top edge
outRect.top = mSpacing;
}
outRect.bottom = mSpacing; // item bottom
} else {
// column * ((1f / spanCount) * spacing)
outRect.left = column * mSpacing / mSpanCount;
// spacing - (column + 1) * ((1f / spanCount) * spacing)
outRect.right = mSpacing - (column + 1) * mSpacing / mSpanCount;
if (position >= mSpanCount) {
outRect.top = mSpacing; // item top
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
final int count = parent.getChildCount();
int start = 0;
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
final View child = parent.getChildAt(translatedChildPosition(parent, layoutPos));
final int adapterPos = parent.getChildAdapterPosition(child);
final long key = adapter.getHeaderId(adapterPos);
if (key == StickyHeaderAdapter.NO_HEADER_ID) {
start = layoutPos + 1;
}
if (adapterPos != RecyclerView.NO_POSITION && ((layoutPos == start && 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();
}
}
}
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (parent.getLayoutManager() != null && this.mDivider != null) {
if (mOrientation == 1) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state)
{
int position = parent.getChildAdapterPosition(view);
int headerHeight = 0;
if (position != RecyclerView.NO_POSITION && hasHeader(parent, adapter, position)) {
View header = getHeader(parent, adapter, position).itemView;
headerHeight = getHeaderHeightForLayout(header);
}
outRect.set(0, headerHeight, 0, 0);
}
@Override
public int computeVerticalScrollExtent(RecyclerView.State state) {
if (getOrientation() == RecyclerView.VERTICAL) {
return super.computeVerticalScrollExtent(state);
}
return computeVerticalScrollExtent();
}
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
if (onInterceptAdjustPagingAfterLayoutComplete())
return;
if (mAdjustPagingAfterLayoutComplete) {
mAdjustPagingAfterLayoutComplete = false;
if (mPagingEnable) {
adjustPaging(false);
} else {
if (mRecoveryPosition >= 0) {
final int position = mRecoveryPosition;
mRecoveryPosition = -1;
final RecyclerView view = getRecyclerView();
if (view == null)
return;
final View target = findViewByPosition(position);
if (target == null)
return;
final int orientation = getOrientation();
if (orientation == HORIZONTAL) {
if (target.getLeft() == mRecoveryStart)
return;
view.scrollBy(target.getLeft() - mRecoveryStart, 0);
} else {
if (target.getTop() == mRecoveryStart)
return;
view.scrollBy(0, target.getTop() - mRecoveryStart);
}
}
}
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
// layout basically just gets drawn on the reserved space on top of the first view
mView.layout(parent.getLeft(), 0, parent.getRight(), mView.getMeasuredHeight());
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
if (parent.getChildAdapterPosition(view) == 0) {
c.save();
if (mHorizontal) {
c.clipRect(parent.getLeft(), parent.getTop(), view.getLeft(), parent.getBottom());
final int width = mView.getMeasuredWidth();
final float left = (view.getLeft() - width) * mParallax;
c.translate(left, 0);
mView.draw(c);
if (mShadowSize > 0) {
c.translate(view.getLeft() - left - mShadowSize, 0);
c.drawRect(parent.getLeft(), parent.getTop(), mShadowSize, parent.getBottom(), mShadowPaint);
}
} else {
c.clipRect(parent.getLeft(), parent.getTop(), parent.getRight(), view.getTop());
final int height = mView.getMeasuredHeight();
final float top = (view.getTop() - height) * mParallax;
c.translate(0, top);
mView.draw(c);
if (mShadowSize > 0) {
c.translate(0, view.getTop() - top - mShadowSize);
c.drawRect(parent.getLeft(), parent.getTop(), parent.getRight(), mShadowSize, mShadowPaint);
}
}
c.restore();
break;
}
}
}
@Override
public void onDrawViewImpl(Canvas canvas, View view, RecyclerView.ViewHolder holder, RecyclerView parent, RecyclerView.State state) {
canvas.save();
parent.getDecoratedBoundsWithMargins(view, tempBounds);
divider.setBounds(tempBounds.left, tempBounds.bottom - divider.getIntrinsicHeight(), tempBounds.right, tempBounds.bottom);
divider.draw(canvas);
canvas.restore();
}
/**
* @param recycler the object
* @param state the state
*/
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (Exception e) {
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
if (position == 0) {
outRect.left = space;
outRect.right = 0;
} else if (thumbnailsCount > 10 && position == thumbnailsCount - 1) {
outRect.left = 0;
outRect.right = space;
} else {
outRect.left = 0;
outRect.right = 0;
}
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (state.getItemCount() == 0) {
removeAndRecycleAllViews(recycler);
return;
}
onceCompleteScrollLength = -1;
//分离全部已有的view,放入临时缓存
detachAndScrapAttachedViews(recycler);
fill(recycler, state, 0);
}
public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
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 left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top + SIZE_PADDING, right, bottom - SIZE_PADDING);
mDivider.draw(c);
}
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
GallerySmoothScroller linearSmoothScroller = new GallerySmoothScroller(recyclerView.getContext());
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
final int position = parent.getChildAdapterPosition(view);
@SuppressWarnings("ConstantConditions") final int itemCount = parent.getAdapter().getItemCount();
final RecyclerView.LayoutManager manager = parent.getLayoutManager();
final int type = position == 0 ? TYPE_FIRST :
(position == itemCount - 1 ? TYPE_LAST : TYPE_NORMAL);
if (manager instanceof LinearLayoutManager &&
((LinearLayoutManager) manager).getOrientation() == LinearLayoutManager.HORIZONTAL) {
outRect.top = mGap * 2;
outRect.bottom = mGap * 2;
switch (type) {
default:
case TYPE_NORMAL:
outRect.left = mGap;
outRect.right = mGap;
break;
case TYPE_FIRST:
outRect.left = mGap * 2;
outRect.right = mGap;
break;
case TYPE_LAST:
outRect.left = mGap;
outRect.right = mGap * 2;
break;
}
} else {
outRect.left = mGap * 2;
outRect.right = mGap * 2;
switch (type) {
default:
case TYPE_NORMAL:
outRect.top = mGap;
outRect.bottom = mGap;
break;
case TYPE_FIRST:
outRect.top = mGap * 2;
outRect.bottom = mGap;
break;
case TYPE_LAST:
outRect.top = mGap;
outRect.bottom = mGap * 2;
break;
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1)
outRect.right = getSpace();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mDivider != null && mDrawOver) {
draw(c, parent);
}
}
/**
* {@inheritDoc}
*/
@Override
public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
final int count = parent.getChildCount();
boolean headerDrawn = false;
for (int layoutPos = 0; layoutPos < count; layoutPos++) {
final View child = parent.getChildAt(layoutPos);
boolean visible = getAnimatedTop(child) > -child.getHeight();
final int adapterPos = parent.getChildAdapterPosition(child);
if (visible && adapterPos != RecyclerView.NO_POSITION
&& (!headerDrawn || hasSubHeader(adapterPos))) {
int left, top;
final View header = getHeader(parent, adapterPos).itemView;
final View subHeader = getSubHeader(parent, adapterPos).itemView;
canvas.save();
left = child.getLeft();
top = getSubHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
canvas.translate(left, top);
subHeader.setTranslationX(left);
subHeader.setTranslationY(top);
subHeader.draw(canvas);
canvas.restore();
if (!headerDrawn || hasHeader(adapterPos)) {
canvas.save();
left = child.getLeft();
top = getHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
canvas.translate(left, top);
header.setTranslationX(left);
header.setTranslationY(top);
header.draw(canvas);
canvas.restore();
}
headerDrawn = true;
}
}
}