下面列出了androidx.recyclerview.widget.RecyclerView#NO_ID 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
long getGlobalItemId(int localPosition, long defaultGlobalValue) {
final long localItemId = adapter.getItemId(localPosition);
if (RecyclerView.NO_ID == localItemId) {
return RecyclerView.NO_ID;
}
final Long globalItemId = localItemIdMap.get(localItemId);
if (globalItemId == null) {
localItemIdMap.put(localItemId, defaultGlobalValue);
return defaultGlobalValue;
}
return globalItemId;
}
@SuppressWarnings("WeakerAccess") /* synthetic access */
void onAttached(@NonNull View view) {
RecyclerView.ViewHolder holder = mRecyclerView.findContainingViewHolder(view);
if (holder == null) {
if (DEBUG) {
Log.w(TAG, "Unable to find ViewHolder for View. Ignoring onAttached event.");
}
return;
}
int position = holder.getAbsoluteAdapterPosition();
long id = holder.getItemId();
if (position != RecyclerView.NO_POSITION && id != RecyclerView.NO_ID) {
mPositionToKey.put(position, id);
mKeyToPosition.put(id, position);
}
}
@SuppressWarnings("WeakerAccess") /* synthetic access */
void onDetached(@NonNull View view) {
RecyclerView.ViewHolder holder = mRecyclerView.findContainingViewHolder(view);
if (holder == null) {
if (DEBUG) {
Log.w(TAG, "Unable to find ViewHolder for View. Ignoring onDetached event.");
}
return;
}
int position = holder.getAbsoluteAdapterPosition();
long id = holder.getItemId();
if (position != RecyclerView.NO_POSITION && id != RecyclerView.NO_ID) {
mPositionToKey.delete(position);
mKeyToPosition.remove(id);
}
}
/**
* Replaces the <code>stickyHeader</code> with the header associated with
* the <code>headerId</code>.
*
* @param headerId The id for the header view
*/
protected void performHeaderSwap(long headerId) {
//If we don't have a valid headerId then clear the current header
if (headerId == RecyclerView.NO_ID) {
clearStickyHeader();
return;
}
//Get the position of the associated header
int headerPosition = getHeaderPosition(headerId);
if (headerPosition == RecyclerView.NO_POSITION) {
return;
}
updateHeader(headerId, headerPosition);
}
@Override
public long getItemId(int position) {
if (position == super.getItemCount()) {
return RecyclerView.NO_ID;
}
return super.getItemId(position);
}
@Override
public long getItemId(int position) {
if (position >= 0 && position < dataset.size()) {
return dataset.get(position);
}
return RecyclerView.NO_ID;
}
@Override
public long getItemId(int position) {
if (!hasStableIds()) {
return RecyclerView.NO_ID;
}
return this.getItem(position).getId();
}
/**
* Returns the total number of views that are associated with the specified
* header id. If the headerId doesn't exist then 0 will be returned.
*
* @param headerId The headerId to find the number of children for
* @return The number of children views associated with the given <code>headerId</code>
*/
public int getChildCount(long headerId) {
if (headerId == RecyclerView.NO_ID) {
return 0;
}
HeaderItem headerItem = headerData.headerItems.get(headerId);
return headerItem != null ? headerItem.getChildCount() : 0;
}
/**
* Determines the adapter position for the header associated with
* the <code>headerId</code>
*
* @param headerId The id to find the header for
* @return The associated headers position or {@link RecyclerView#NO_POSITION}
*/
public int getHeaderPosition(long headerId) {
if (headerId == RecyclerView.NO_ID) {
return RecyclerView.NO_POSITION;
}
HeaderItem headerItem = headerData.headerItems.get(headerId);
return headerItem != null ? headerItem.getAdapterPosition() : RecyclerView.NO_POSITION;
}
/**
* Calculates the information necessary to display headers
* using the associated {@link DataSource}
*
* @param reuseData A {@link HeaderData} that will be reused instead of creating a new one
* @param dataSource The data to use when calculating the header information
* @return The <code>reuseData</code> that has been populated with the header information
*/
@NonNull
public HeaderData calculate(@NonNull HeaderData reuseData, @NonNull DataSource dataSource) {
reuseData.headerItems.clear();
reuseData.adapterPositionItemMap.clear();
HeaderItem currentHeader = null;
for (int i = 0; i < dataSource.getChildCount(); i++) {
int adapterPosition = i + (reuseData.showHeaderAsChild ? 0 : reuseData.headerItems.size());
long headerId = dataSource.getHeaderId(i);
if (headerId == RecyclerView.NO_ID) {
currentHeader = null;
reuseData.adapterPositionItemMap.put(adapterPosition, new AdapterItem(null, i, headerApi.getChildViewType(i) & ~HeaderApi.HEADER_VIEW_TYPE_MASK));
continue;
}
//Adds new headers to the list when detected
if (currentHeader == null || currentHeader.getId() != headerId) {
currentHeader = new HeaderItem(headerId, adapterPosition);
reuseData.headerItems.put(headerId, currentHeader);
reuseData.adapterPositionItemMap.put(adapterPosition, new AdapterItem(currentHeader, i, headerApi.getHeaderViewType(i) | HeaderApi.HEADER_VIEW_TYPE_MASK));
if (reuseData.showHeaderAsChild) {
currentHeader.setChildCount(1);
continue;
}
adapterPosition++;
}
currentHeader.setChildCount(currentHeader.getChildCount() + 1);
reuseData.adapterPositionItemMap.put(adapterPosition, new AdapterItem(null, i, headerApi.getChildViewType(i) & ~HeaderApi.HEADER_VIEW_TYPE_MASK));
}
return reuseData;
}
/**
* Retrieves the id for the header associated with the <code>childPosition</code> from
* the specified <code>headerAdapter</code>
*
* @param childPosition The child position associated with the header
* @return The id for the header or {@link RecyclerView#NO_ID}
*/
protected long getHeaderId(int childPosition) {
if (childPosition < 0 || childPosition >= headerApi.getChildCount()) {
return RecyclerView.NO_ID;
}
return headerApi.getHeaderId(childPosition);
}
public void update() {
saveOldState();
adapter.update();
int start = layoutManager.findFirstVisibleItemPosition();
int end = layoutManager.findLastVisibleItemPosition();
long scrollToItemId = RecyclerView.NO_ID;
int offset = 0;
for (int i = start; i <= end; i++) {
if (adapter.getItemId(i) != RecyclerView.NO_ID) {
View v = layoutManager.findViewByPosition(i);
if (v != null) {
scrollToItemId = adapter.getItemId(i);
offset = v.getTop();
break;
}
}
}
DiffUtil.calculateDiff(this).dispatchUpdatesTo(adapter);
if (scrollToItemId != RecyclerView.NO_ID) {
int position = -1;
for (int i = 0; i < adapter.getItemCount(); i++) {
if (adapter.getItemId(i) == scrollToItemId) {
position = i;
break;
}
}
if (position > 0) {
layoutManager.scrollToPositionWithOffset(position, offset);
}
}
}
public void reset() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
resetImageDrawables();
id = RecyclerView.NO_ID;
watching = false;
currentDrawable = checkInDrawable;
invalidateSelf();
}
}
public void update() {
saveOldState();
adapter.update();
int start = layoutManager.findFirstVisibleItemPosition();
int end = layoutManager.findLastVisibleItemPosition();
long scrollToItemId = RecyclerView.NO_ID;
int offset = 0;
for (int i = start; i <= end; i++) {
if (adapter.getItemId(i) != RecyclerView.NO_ID) {
View v = layoutManager.findViewByPosition(i);
if (v != null) {
scrollToItemId = adapter.getItemId(i);
offset = v.getTop();
break;
}
}
}
DiffUtil.calculateDiff(this).dispatchUpdatesTo(adapter);
if (scrollToItemId != RecyclerView.NO_ID) {
int position = -1;
for (int i = 0; i < adapter.getItemCount(); i++) {
if (adapter.getItemId(i) == scrollToItemId) {
position = i;
break;
}
}
if (position > 0) {
layoutManager.scrollToPositionWithOffset(position, offset);
}
}
}
@Override
public long getItemId(int position) {
Item item = getItem(position);
return item != null ? item.getLongId() : RecyclerView.NO_ID;
}
@Override
public long getHeaderId(int childPosition) {
return RecyclerView.NO_ID;
}
@Override
public long getHeaderId(int childPosition) {
return RecyclerView.NO_ID;
}
protected long getItemId(Type item) {
return RecyclerView.NO_ID;
}