Skip to content

Commit

Permalink
去除头部和尾部view的复用能力
Browse files Browse the repository at this point in the history
  • Loading branch information
朱凯 committed Feb 1, 2021
1 parent 7fd909a commit ff060ea
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions adapter/src/main/java/com/zhukai/adapter/VastAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -238,7 +244,7 @@ public final int getItemCount() {
if (isVacancyVisibility()) {
return 1;
}
return getHeaderCount() + getFooterCount() + getDataSize();
return getHeaderCount() + getFooterCount() + getDataCount();
}

/**
Expand All @@ -252,22 +258,15 @@ 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();
}

/**
* 是否展示无列表提示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;
}

/**
Expand Down Expand Up @@ -483,7 +482,7 @@ public interface OnPreloadListener {
/**
* Holder创建完成
*/
public void onCreateHolder(VastHolder holder){
public void onCreateHolder(VastHolder holder) {

}

Expand All @@ -501,4 +500,11 @@ public int getItemViewIndex(int position) {
return 0;
}

/**
* 获取数据总量
*/
public int getDataCount() {
return mData.size();
}

}

0 comments on commit ff060ea

Please sign in to comment.