From ff060ea742b404ec652acbfbaa7b37d47e27a196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=87=AF?= Date: Mon, 1 Feb 2021 10:44:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=A4=B4=E9=83=A8=E5=92=8C?= =?UTF-8?q?=E5=B0=BE=E9=83=A8view=E7=9A=84=E5=A4=8D=E7=94=A8=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/zhukai/adapter/VastAdapter.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/adapter/src/main/java/com/zhukai/adapter/VastAdapter.java b/adapter/src/main/java/com/zhukai/adapter/VastAdapter.java index 28c15d7..3fc8427 100644 --- a/adapter/src/main/java/com/zhukai/adapter/VastAdapter.java +++ b/adapter/src/main/java/com/zhukai/adapter/VastAdapter.java @@ -125,18 +125,24 @@ public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) { super.onViewAttachedToWindow(holder); } + private VastHolder clearHolderRecyclable(VastHolder vastHolder){ + vastHolder.setIsRecyclable(false); + return vastHolder; + } + @NonNull @Override public final RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (isVacancyVisibility()) { - return new VastHolder(mVacancyHintView); + return clearHolderRecyclable(new VastHolder(mVacancyHintView)); } + int position = positionFlag(viewType); if (isHeader(position)) { - return new VastHolder(mHeaderViews.get(position)); + return clearHolderRecyclable(new VastHolder(mHeaderViews.get(position))); } if (isFooter(position)) { - return new VastHolder(mFooterViews.get(position - getDataSize() - getHeaderCount())); + return clearHolderRecyclable(new VastHolder(mFooterViews.get(position - getDataCount() - getHeaderCount()))); } final VastHolder vastHolder = new VastHolder(LayoutInflater.from(parent.getContext()).inflate(layoutIds[viewType], parent, false)); @@ -211,7 +217,7 @@ public final void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int private boolean checkPreload(int position) { return null != mOnPreloadListener && mHostRv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE - && Math.max(getDataSize() - 1 - position, 0) == mPreloadThreshold; + && Math.max(getDataCount() - 1 - position, 0) == mPreloadThreshold; } @Override @@ -238,7 +244,7 @@ public final int getItemCount() { if (isVacancyVisibility()) { return 1; } - return getHeaderCount() + getFooterCount() + getDataSize(); + return getHeaderCount() + getFooterCount() + getDataCount(); } /** @@ -252,7 +258,7 @@ private boolean isHeader(int position) { * 是否是尾view */ private boolean isFooter(int position) { - int frontCount = getHeaderCount() + getDataSize(); + int frontCount = getHeaderCount() + getDataCount(); return position >= frontCount && position - frontCount < getFooterCount(); } @@ -260,14 +266,7 @@ private boolean isFooter(int position) { * 是否展示无列表提示View */ private boolean isVacancyVisibility() { - return (mHeaderViews.size() + mFooterViews.size() + getDataSize()) < 1 && null != mVacancyHintView; - } - - /** - * 获取数据总量 - */ - public int getDataSize() { - return mData.size(); + return (mHeaderViews.size() + mFooterViews.size() + getDataCount()) < 1 && null != mVacancyHintView; } /** @@ -483,7 +482,7 @@ public interface OnPreloadListener { /** * Holder创建完成 */ - public void onCreateHolder(VastHolder holder){ + public void onCreateHolder(VastHolder holder) { } @@ -501,4 +500,11 @@ public int getItemViewIndex(int position) { return 0; } + /** + * 获取数据总量 + */ + public int getDataCount() { + return mData.size(); + } + }