gpt4 book ai didi

java - ViewHolder中View类型的字段itemView引用了什么?

转载 作者:行者123 更新时间:2023-12-02 01:41:16 25 4
gpt4 key购买 nike

我试图弄清楚字段 itemView 实际上是什么。这是我从文档中获取的代码 fragment 。

public abstract static class ViewHolder {
@NonNull
public final View itemView;
// others removed for simplicity
}

如果我们扩展 ViewHolder 类,因为我们还需要指定一些从 View 派生的类型字段来为每个列表项保存我们自己的小部件,那么明确 itemView 不会保存我们自己的小部件。

问题

itemView 字段引用什么?我很困惑它是否引用了 RecyclerView 还是 list_item_layout.xml 的 Root View (包含我们自己的小部件)。你能澄清一下是哪一个吗?

编辑

对于那些想了解ViewHolder内部细节的人,请参阅以下内容。我提供完整的。

public abstract static class ViewHolder {
@NonNull
public final View itemView;
WeakReference<RecyclerView> mNestedRecyclerView;
int mPosition = -1;
int mOldPosition = -1;
long mItemId = -1L;
int mItemViewType = -1;
int mPreLayoutPosition = -1;
RecyclerView.ViewHolder mShadowedHolder = null;
RecyclerView.ViewHolder mShadowingHolder = null;
static final int FLAG_BOUND = 1;
static final int FLAG_UPDATE = 2;
static final int FLAG_INVALID = 4;
static final int FLAG_REMOVED = 8;
static final int FLAG_NOT_RECYCLABLE = 16;
static final int FLAG_RETURNED_FROM_SCRAP = 32;
static final int FLAG_IGNORE = 128;
static final int FLAG_TMP_DETACHED = 256;
static final int FLAG_ADAPTER_POSITION_UNKNOWN = 512;
static final int FLAG_ADAPTER_FULLUPDATE = 1024;
static final int FLAG_MOVED = 2048;
static final int FLAG_APPEARED_IN_PRE_LAYOUT = 4096;
static final int PENDING_ACCESSIBILITY_STATE_NOT_SET = -1;
static final int FLAG_BOUNCED_FROM_HIDDEN_LIST = 8192;
static final int FLAG_SET_A11Y_ITEM_DELEGATE = 16384;
int mFlags;
private static final List<Object> FULLUPDATE_PAYLOADS = Collections.emptyList();
List<Object> mPayloads = null;
List<Object> mUnmodifiedPayloads = null;
private int mIsRecyclableCount = 0;
RecyclerView.Recycler mScrapContainer = null;
boolean mInChangeScrap = false;
private int mWasImportantForAccessibilityBeforeHidden = 0;
@VisibleForTesting
int mPendingAccessibilityState = -1;
RecyclerView mOwnerRecyclerView;

public ViewHolder(@NonNull View itemView) {
if (itemView == null) {
throw new IllegalArgumentException("itemView may not be null");
} else {
this.itemView = itemView;
}
}

void flagRemovedAndOffsetPosition(int mNewPosition, int offset, boolean applyToPreLayout) {
this.addFlags(8);
this.offsetPosition(offset, applyToPreLayout);
this.mPosition = mNewPosition;
}

void offsetPosition(int offset, boolean applyToPreLayout) {
if (this.mOldPosition == -1) {
this.mOldPosition = this.mPosition;
}

if (this.mPreLayoutPosition == -1) {
this.mPreLayoutPosition = this.mPosition;
}

if (applyToPreLayout) {
this.mPreLayoutPosition += offset;
}

this.mPosition += offset;
if (this.itemView.getLayoutParams() != null) {
((RecyclerView.LayoutParams)this.itemView.getLayoutParams()).mInsetsDirty = true;
}

}

void clearOldPosition() {
this.mOldPosition = -1;
this.mPreLayoutPosition = -1;
}

void saveOldPosition() {
if (this.mOldPosition == -1) {
this.mOldPosition = this.mPosition;
}

}

boolean shouldIgnore() {
return (this.mFlags & 128) != 0;
}

/** @deprecated */
@Deprecated
public final int getPosition() {
return this.mPreLayoutPosition == -1 ? this.mPosition : this.mPreLayoutPosition;
}

public final int getLayoutPosition() {
return this.mPreLayoutPosition == -1 ? this.mPosition : this.mPreLayoutPosition;
}

public final int getAdapterPosition() {
return this.mOwnerRecyclerView == null ? -1 : this.mOwnerRecyclerView.getAdapterPositionFor(this);
}

public final int getOldPosition() {
return this.mOldPosition;
}

public final long getItemId() {
return this.mItemId;
}

public final int getItemViewType() {
return this.mItemViewType;
}

boolean isScrap() {
return this.mScrapContainer != null;
}

void unScrap() {
this.mScrapContainer.unscrapView(this);
}

boolean wasReturnedFromScrap() {
return (this.mFlags & 32) != 0;
}

void clearReturnedFromScrapFlag() {
this.mFlags &= -33;
}

void clearTmpDetachFlag() {
this.mFlags &= -257;
}

void stopIgnoring() {
this.mFlags &= -129;
}

void setScrapContainer(RecyclerView.Recycler recycler, boolean isChangeScrap) {
this.mScrapContainer = recycler;
this.mInChangeScrap = isChangeScrap;
}

boolean isInvalid() {
return (this.mFlags & 4) != 0;
}

boolean needsUpdate() {
return (this.mFlags & 2) != 0;
}

boolean isBound() {
return (this.mFlags & 1) != 0;
}

boolean isRemoved() {
return (this.mFlags & 8) != 0;
}

boolean hasAnyOfTheFlags(int flags) {
return (this.mFlags & flags) != 0;
}

boolean isTmpDetached() {
return (this.mFlags & 256) != 0;
}

boolean isAdapterPositionUnknown() {
return (this.mFlags & 512) != 0 || this.isInvalid();
}

void setFlags(int flags, int mask) {
this.mFlags = this.mFlags & ~mask | flags & mask;
}

void addFlags(int flags) {
this.mFlags |= flags;
}

void addChangePayload(Object payload) {
if (payload == null) {
this.addFlags(1024);
} else if ((this.mFlags & 1024) == 0) {
this.createPayloadsIfNeeded();
this.mPayloads.add(payload);
}

}

private void createPayloadsIfNeeded() {
if (this.mPayloads == null) {
this.mPayloads = new ArrayList();
this.mUnmodifiedPayloads = Collections.unmodifiableList(this.mPayloads);
}

}

void clearPayload() {
if (this.mPayloads != null) {
this.mPayloads.clear();
}

this.mFlags &= -1025;
}

List<Object> getUnmodifiedPayloads() {
if ((this.mFlags & 1024) == 0) {
return this.mPayloads != null && this.mPayloads.size() != 0 ? this.mUnmodifiedPayloads : FULLUPDATE_PAYLOADS;
} else {
return FULLUPDATE_PAYLOADS;
}
}

void resetInternal() {
this.mFlags = 0;
this.mPosition = -1;
this.mOldPosition = -1;
this.mItemId = -1L;
this.mPreLayoutPosition = -1;
this.mIsRecyclableCount = 0;
this.mShadowedHolder = null;
this.mShadowingHolder = null;
this.clearPayload();
this.mWasImportantForAccessibilityBeforeHidden = 0;
this.mPendingAccessibilityState = -1;
RecyclerView.clearNestedRecyclerViewIfNotNested(this);
}

void onEnteredHiddenState(RecyclerView parent) {
if (this.mPendingAccessibilityState != -1) {
this.mWasImportantForAccessibilityBeforeHidden = this.mPendingAccessibilityState;
} else {
this.mWasImportantForAccessibilityBeforeHidden = ViewCompat.getImportantForAccessibility(this.itemView);
}

parent.setChildImportantForAccessibilityInternal(this, 4);
}

void onLeftHiddenState(RecyclerView parent) {
parent.setChildImportantForAccessibilityInternal(this, this.mWasImportantForAccessibilityBeforeHidden);
this.mWasImportantForAccessibilityBeforeHidden = 0;
}

public String toString() {
StringBuilder sb = new StringBuilder("ViewHolder{" + Integer.toHexString(this.hashCode()) + " position=" + this.mPosition + " id=" + this.mItemId + ", oldPos=" + this.mOldPosition + ", pLpos:" + this.mPreLayoutPosition);
if (this.isScrap()) {
sb.append(" scrap ").append(this.mInChangeScrap ? "[changeScrap]" : "[attachedScrap]");
}

if (this.isInvalid()) {
sb.append(" invalid");
}

if (!this.isBound()) {
sb.append(" unbound");
}

if (this.needsUpdate()) {
sb.append(" update");
}

if (this.isRemoved()) {
sb.append(" removed");
}

if (this.shouldIgnore()) {
sb.append(" ignored");
}

if (this.isTmpDetached()) {
sb.append(" tmpDetached");
}

if (!this.isRecyclable()) {
sb.append(" not recyclable(" + this.mIsRecyclableCount + ")");
}

if (this.isAdapterPositionUnknown()) {
sb.append(" undefined adapter position");
}

if (this.itemView.getParent() == null) {
sb.append(" no parent");
}

sb.append("}");
return sb.toString();
}

public final void setIsRecyclable(boolean recyclable) {
this.mIsRecyclableCount = recyclable ? this.mIsRecyclableCount - 1 : this.mIsRecyclableCount + 1;
if (this.mIsRecyclableCount < 0) {
this.mIsRecyclableCount = 0;
Log.e("View", "isRecyclable decremented below 0: unmatched pair of setIsRecyable() calls for " + this);
} else if (!recyclable && this.mIsRecyclableCount == 1) {
this.mFlags |= 16;
} else if (recyclable && this.mIsRecyclableCount == 0) {
this.mFlags &= -17;
}

}

public final boolean isRecyclable() {
return (this.mFlags & 16) == 0 && !ViewCompat.hasTransientState(this.itemView);
}

boolean shouldBeKeptAsChild() {
return (this.mFlags & 16) != 0;
}

boolean doesTransientStatePreventRecycling() {
return (this.mFlags & 16) == 0 && ViewCompat.hasTransientState(this.itemView);
}

boolean isUpdated() {
return (this.mFlags & 2) != 0;
}
}

最佳答案

这完全取决于您的代码,因为当您调用 new SomeViewHolder(itemView) 时,它会引用 itemView

例如Android指南-使用RecyclerView创建列表-Add a list adapter的代码中MyViewHolder 创建后有一个 TextView v 的引用。

public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
...
MyViewHolder vh = new MyViewHolder(v);
return vh;
}

关于java - ViewHolder中View类型的字段itemView引用了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com