下面列出了androidx.recyclerview.widget.RecyclerView#getChildAdapterPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
public final boolean shouldDrawDividerBelow(View view, RecyclerView parent) {
if (!(parent.getAdapter() instanceof PreferenceGroupAdapter)) {
return false;
}
PreferenceGroupAdapter adapter = (PreferenceGroupAdapter) parent.getAdapter();
int index = parent.getChildAdapterPosition(view);
if (index == RecyclerView.NO_POSITION) {
return false;
}
Preference preference = adapter.getItem(index);
switch (preference.getDividerBelowVisibility()) {
case DividerVisibility.ENFORCED:
return true;
case DividerVisibility.FORBIDDEN:
return false;
case DividerVisibility.UNSPECIFIED:
default:
if (index == adapter.getItemCount() - 1) {
return isAllowDividerAfterLastItem();
}
return shouldDrawDividerBelow(view, parent, adapter, index, preference);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
int column = position % spanCount;
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount;
outRect.right = (column + 1) * spacing / spanCount;
if (position < spanCount) {
outRect.top = spacing;
}
outRect.bottom = spacing;
} else {
outRect.left = column * spacing / spanCount;
outRect.right = spacing - (column + 1) * spacing / spanCount;
if (position < spanCount) {
outRect.top = spacing;
}
outRect.bottom = spacing;
}
}
@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 getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item 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();
}
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
if (parent instanceof SuperRecyclerView) {
SuperRecyclerView superRecyclerView = (SuperRecyclerView) parent;
if (superRecyclerView.getHeaderContainer().getChildCount() > position) {
return;
}
position -= superRecyclerView.getHeaderContainer().getChildCount();
}
int column = position % spanCount; // item column
outRect.top = verticalSize;
if (column == spanCount - 1) {
if (includeEdge) {
outRect.right = horizontalSize;
}
outRect.left = horizontalSize / 2;
} else if (column == 0) {
if (includeEdge) {
outRect.left = horizontalSize;
}
outRect.right = horizontalSize / 2;
} else {
outRect.left = horizontalSize / 2;
outRect.right = horizontalSize / 2;
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION) {
return;
}
if (mHeaderPositionCalculator.hasNewHeader(itemPosition, mOrientationProvider.isReverseLayout(parent))) {
View header = getHeaderView(parent, itemPosition);
setItemOffsetsForHeader(outRect, header, mOrientationProvider.getOrientation(parent));
}
}
public void restoreElevation(RecyclerView recyclerView){
for(int i=0; i < recyclerView.getChildCount(); i++){
final View childAt = recyclerView.getChildAt(i);
Utils.setElevation(childAt,5);
if(i == recyclerView.getChildAdapterPosition(childAt)){
Utils.setElevation(childAt,30);
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
final int position = parent.getChildAdapterPosition(view);
// get the current number of columns for the grid
final int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
final int left = isFirstInRow(position, spanCount) ? 0 : mHalfSpacingOffsetPX;
final int top = isInFirstRow(position, spanCount) ? 0 : mSpacingOffsetPX;
final int right = isLastInRow(position, spanCount) ? 0 : mHalfSpacingOffsetPX;
// the offset for the bottom should always be 0 because the top margin of the next row will apply.
outRect.set(left, top, right, 0);
}
public void initHeaderBounds(@NonNull Rect bounds, @NonNull RecyclerView recyclerView, @NonNull View header, @NonNull View firstView, boolean firstHeader) {
int orientation = mOrientationProvider.getOrientation(recyclerView);
initDefaultHeaderOffset(bounds, recyclerView, header, firstView, orientation);
if (firstHeader && isStickyHeaderBeingPushedOffscreen(recyclerView, header)) {
View viewAfterNextHeader = getFirstViewUnobscuredByHeader(recyclerView, header);
int firstViewUnderHeaderPosition = recyclerView.getChildAdapterPosition(viewAfterNextHeader);
View secondHeader = mHeaderProvider.getHeader(recyclerView, firstViewUnderHeaderPosition);
translateHeaderWithNextHeader(recyclerView, mOrientationProvider.getOrientation(recyclerView), bounds,
header, viewAfterNextHeader, secondHeader);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int dataSize = state.getItemCount();
int position = parent.getChildAdapterPosition(view);
if (dataSize > 0 && position == dataSize - 1) {
outRect.set(0, 0, 0, mBottomOffset);
} else {
outRect.set(0, 0, 0, 0);
}
}
private boolean isStickyView(RecyclerView parent, View v) {
if (v == null) {
return false;
}
int position = parent.getChildAdapterPosition(v);
if (position == RecyclerView.NO_POSITION) {
return false;
}
return isStickyViewType(parent, position, mAdapter.getItemViewType(position));
}
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;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
int indexInAdapter = parent.getChildAdapterPosition(view);
if (indexInAdapter == 0) {
outRect.top += mVerticalPadding;
}
if (indexInAdapter == parent.getAdapter().getItemCount() - 1) {
outRect.bottom += mVerticalPadding;
}
}
@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
boolean foundFirstChild = false;
float viewVerticalCenter = 0f;
float left = 0f;
float limit = -(layout.getMeasuredWidth() / 2f);
float initialPosition =
((headerSize / 2f) - (layout.getMeasuredWidth() / 2f) - (margin / 2f)) * PARALLAX_RATIO;
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
if (viewVerticalCenter == 0f) {
viewVerticalCenter =
view.getTop() + (view.getMeasuredHeight() / 2f) - (layout.getMeasuredWidth() / 2f);
}
if (parent.getChildAdapterPosition(view) == 0) {
left =
view.getLeft() - (headerSize / 2f) - (layout.getMeasuredWidth() / 2f) - (margin / 2f);
left *= PARALLAX_RATIO;
if (left < limit) {
left = limit;
}
foundFirstChild = true;
break;
}
}
if (!foundFirstChild) {
left = limit;
}
// With API <= 23, there's a bug where if alpha is set to 0, TextViews' alpha is not set correctly
int movementPercentage = Math.max((int) (getPercentage(limit, initialPosition, left) * 255), 1);
c.save();
c.saveLayerAlpha(new RectF(0, 0, headerSize - 2, c.getHeight()), movementPercentage,
Canvas.ALL_SAVE_FLAG);
c.translate(left, viewVerticalCenter - VERTICAL_OFFSET);
layout.draw(c);
c.restore();
}
protected int getAdapterPosition(RecyclerView parent, View view) {
int childPosition = parent.getChildAdapterPosition(view);
if (parent instanceof OmegaRecyclerView) {
RecyclerView.Adapter adapter = parent.getAdapter();
if (adapter instanceof HeaderFooterWrapperAdapter) {
return ((HeaderFooterWrapperAdapter) adapter).applyChildPositionToRealPosition(childPosition);
}
}
return childPosition;
}
@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.bottom = getSpace();
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) == 0)
outRect.left = getSpace();
}