下面列出了androidx.recyclerview.widget.RecyclerView#NO_POSITION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void checkScrollForLoad(boolean scroll) {
if (chatLayoutManager == null || paused) {
return;
}
int firstVisibleItem = chatLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = firstVisibleItem == RecyclerView.NO_POSITION ? 0 : Math.abs(chatLayoutManager.findLastVisibleItemPosition() - firstVisibleItem) + 1;
if (visibleItemCount > 0) {
int totalItemCount = chatAdapter.getItemCount();
int checkLoadCount;
if (scroll) {
checkLoadCount = 25;
} else {
checkLoadCount = 5;
}
if (firstVisibleItem <= checkLoadCount && !loading && !endReached) {
loadMessages(false);
}
}
}
@Override
public void itemSwiped(int position, int direction) {
ContentItem item = itemAdapter.getAdapterItem(position);
item.setSwipeDirection(direction);
if (item.getContent() != null) {
Debouncer<Content> deleteDebouncer = new Debouncer<>(2000, this::onDeleteBook);
deleteDebouncer.submit(item.getContent());
Runnable cancelSwipe = () -> {
deleteDebouncer.clear();
item.setSwipeDirection(0);
int position1 = itemAdapter.getAdapterPosition(item);
if (position1 != RecyclerView.NO_POSITION)
fastAdapter.notifyItemChanged(position1);
};
item.setUndoSwipeAction(cancelSwipe);
fastAdapter.notifyItemChanged(position);
}
}
static <K> FocusDelegate<K> dummy() {
return new FocusDelegate<K>() {
@Override
public void focusItem(@NonNull ItemDetails<K> item) {
}
@Override
public boolean hasFocusedItem() {
return false;
}
@Override
public int getFocusedPosition() {
return RecyclerView.NO_POSITION;
}
@Override
public void clearFocus() {
}
};
}
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
TupleFolderNav folder = items.get(pos);
if (folder == null)
return;
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGES)
.putExtra("account", folder.account)
.putExtra("folder", folder.id)
.putExtra("type", folder.type));
}
@Override
public void itemSwiped(int position, int direction) {
ContentItem item = getItemAdapter().getAdapterItem(position);
item.setSwipeDirection(direction);
if (item.getContent() != null) {
Debouncer<ContentItem> deleteDebouncer = new Debouncer<>(2000, this::onDeleteSwipedBook);
deleteDebouncer.submit(item);
Runnable cancelSwipe = () -> {
deleteDebouncer.clear();
item.setSwipeDirection(0);
int position1 = getItemAdapter().getAdapterPosition(item);
if (position1 != RecyclerView.NO_POSITION)
fastAdapter.notifyItemChanged(position1);
};
item.setUndoSwipeAction(cancelSwipe);
fastAdapter.notifyItemChanged(position);
}
}
@Override
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof RecyclerView.ViewHolder) {
int flatPosition = ((RecyclerView.ViewHolder) v.getTag()).getAdapterPosition();
if (flatPosition == RecyclerView.NO_POSITION) {
return;
}
long expandablePosition = mExpandableItemManager.getExpandablePosition(flatPosition);
int groupPosition = RecyclerViewExpandableItemManager
.getPackedPositionGroup(expandablePosition);
// int childPosition = RecyclerViewExpandableItemManager
// .getPackedPositionChild(expandablePosition);
switch (v.getId()) {
case R.id.layout_group_item:
// toggle expanded/collapsed
if (mExpandableItemManager.isGroupExpanded(groupPosition)) {
mExpandableItemManager.collapseGroup(groupPosition);
} else {
mExpandableItemManager.expandGroup(groupPosition);
}
break;
case R.id.img_menu_ups:
if (mOnGroupOtherClickListener != null) {
mOnGroupOtherClickListener.onOtherClick(groupPosition, v);
}
break;
}
}
if (v.getTag() instanceof ChildViewHolder) {
((ChildViewHolder) v.getTag()).switchCompat.toggle();
}
}
@Override
public int getPreferenceAdapterPosition(Preference preference) {
final int size = mPreferenceList.size();
for (int i = 0; i < size; i++) {
final Preference candidate = mPreferenceList.get(i);
if (candidate != null && candidate.equals(preference)) {
return i;
}
}
return RecyclerView.NO_POSITION;
}
private int findItemPositionById(long id) {
final int first = mMessageViewLayoutManager.findFirstVisibleItemPosition();
final int last = mMessageViewLayoutManager.findLastVisibleItemPosition();
if (last == RecyclerView.NO_POSITION) {
return -1;
}
return mMessagesAdapter.findItemPositionById(id, first, last);
}
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 setSelectedLabel(final Label label) {
if ((label == null && this.selectedLabel == null) || (same(label, this.selectedLabel))) {
return;
}
final int previous = getPosition(this.selectedLabel);
final int position = getPosition(label);
this.selectedLabel = label;
if (previous != RecyclerView.NO_POSITION) {
notifyItemChanged(previous);
}
if (position != RecyclerView.NO_POSITION) {
notifyItemChanged(position);
}
}
/**
* Gets the activated position only when mode is {@code SINGLE}.
*
* @return the activated position when {@code SINGLE}. -1 if no item is selected
* @since 1.0.0-b1
*/
public int getActivatedPosition() {
List<Integer> selectedPositions = mAdapter.getSelectedPositions();
if (mAdapter.getMode() == Mode.SINGLE && selectedPositions.size() == 1) {
return selectedPositions.get(0);
}
return RecyclerView.NO_POSITION;
}
private void changeItem(View view) {
int position = mRecyclerView.getChildAdapterPosition(view);
if (position != RecyclerView.NO_POSITION) {
int color = generateColor();
mColors.set(position, color);
notifyItemChanged(position);
}
}
private void updateStickyLayout(int position)
{
if (position == RecyclerView.NO_POSITION)
{
return;
}
RcvSectionWrapper wrapper = (RcvSectionWrapper) mAdapter.getDatas().get(position - mAdapter.getHeadCounts());
mAdapter.onBindSectionLabelView(mHolder, wrapper.getSection(), position);
mCurrentIndicatePosition = position;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION) {
return;
}
if (mHeaderPositionCalculator.hasNewHeader(itemPosition)) {
View header = getHeaderView(parent, itemPosition);
setItemOffsetsForHeader(outRect, header, mOrientationProvider.getOrientation(parent));
}
}
int getConvertedPosition(int sectionedPosition) {
if (isSectionHeaderPosition(sectionedPosition)) {
return RecyclerView.NO_POSITION;
}
int offset = 0;
for (int i = 0; i < sections.size(); i++) {
if (sections.valueAt(i).sectionedPosition > sectionedPosition) {
break;
}
--offset;
}
return sectionedPosition + offset;
}
@Override
public int getScrollOffset() {
int firstItemPosition = getFirstItemPosition();
if (firstItemPosition == RecyclerView.NO_POSITION) {
return 0;
}
int itemHeight = getItemHeight();
int firstItemTop = getFirstItemOffset();
return mView.getPaddingTop() + firstItemPosition * itemHeight - firstItemTop;
}
private int calculateGroupOffset(RecyclerView parent, long currentHeaderId, int nextPosition) {
if (mExpandableAdapter == null) return 0;
int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(nextPosition));
if (adapterPosHere != RecyclerView.NO_POSITION) {
long nextId = mExpandableAdapter.getGroupUniqueId(adapterPosHere);
if (nextId != currentHeaderId && nextId != NO_STICKY_ID) {
View next = parent.getChildAt(nextPosition);
RecyclerView.ViewHolder viewHolder = getGroupHeader(parent, adapterPosHere);
if (viewHolder == null) return OFFSET_NOT_FOUND;
return (int) next.getY() - viewHolder.itemView.getHeight();
}
}
return OFFSET_NOT_FOUND;
}
/**
* 绘制悬浮组
*
* @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();
}
}
public void changeItem(int position, T item) {
if (position != RecyclerView.NO_POSITION && item != null) {
mItemList.set(position, item);
notifyItemChanged(position);
}
}
/**
* Overcomes the situation of returning an unknown position (-1) of ViewHolders created out of
* the LayoutManager (ex. StickyHeaders).
* <p><b>NOTE:</b> Always call this method, instead of {@code getAdapterPosition()}, in case
* of StickyHeaders use case.</p>
*
* @return the Adapter position result of {@link #getAdapterPosition()} OR the backup position
* preset and known, if the previous result was {@link RecyclerView#NO_POSITION}.
* @see #setBackupPosition(int)
* @since 5.0.0-b6
*/
public final int getFlexibleAdapterPosition() {
int position = getAdapterPosition();
if (position == RecyclerView.NO_POSITION) {
position = mBackupPosition;
}
return position;
}