Skip to content

Commit

Permalink
Merge pull request #176 from shichaohui/master
Browse files Browse the repository at this point in the history
Fixed the problem that addHeaderView does not show after removeHeaderView.
  • Loading branch information
CymChad authored Jul 20, 2016
2 parents 6748005 + aab26ef commit 1a32778
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void setOnItemDragListener(OnItemDragListener onItemDragListener) {
}

public int getViewHolderPosition(RecyclerView.ViewHolder viewHolder) {
return viewHolder.getAdapterPosition() - getHeaderViewsCount();
return viewHolder.getAdapterPosition() - getHeaderLayoutCount();
}

public void onItemDragStart(RecyclerView.ViewHolder viewHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerV
private BaseAnimation mSelectAnimation = new AlphaInAnimation();
private LinearLayout mHeaderLayout;
private LinearLayout mFooterLayout;
private LinearLayout mCopyHeaderLayout = null;
private LinearLayout mCopyFooterLayout = null;
private int pageSize = -1;
private View mContentView;
/**
Expand Down Expand Up @@ -244,7 +246,7 @@ public class OnItemChildClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
if (mChildClickListener != null)
mChildClickListener.onItemChildClick(BaseQuickAdapter.this, v, mViewHolder.getLayoutPosition() - getHeaderViewsCount());
mChildClickListener.onItemChildClick(BaseQuickAdapter.this, v, mViewHolder.getLayoutPosition() - getHeaderLayoutCount());
}
}

Expand Down Expand Up @@ -308,7 +310,7 @@ public BaseQuickAdapter(View contentView, List<T> data) {
*/
public void remove(int position) {
mData.remove(position);
notifyItemRemoved(position + getHeaderViewsCount());
notifyItemRemoved(position + getHeaderLayoutCount());

}

Expand Down Expand Up @@ -379,7 +381,8 @@ public T getItem(int position) {
}

/**
* if setHeadView will be return 1 if not will be return 0
* if setHeadView will be return 1 if not will be return 0.
* notice: Deprecated! Use {@link ViewGroup#getChildCount()} of {@link #getHeaderLayout()} to replace.
*
* @return
*/
Expand All @@ -389,7 +392,8 @@ public int getHeaderViewsCount() {
}

/**
* if mFooterLayout will be return 1 or not will be return 0
* if mFooterLayout will be return 1 or not will be return 0.
* notice: Deprecated! Use {@link ViewGroup#getChildCount()} of {@link #getFooterLayout()} to replace.
*
* @return
*/
Expand All @@ -399,21 +403,17 @@ public int getFooterViewsCount() {
}

/**
* if mHeaderLayout is null will be return false or not will be true
*
* @return
* if addHeaderView will be return 1, if not will be return 0
*/
public boolean hasHeaderLayout() {
return mHeaderLayout != null;
public int getHeaderLayoutCount() {
return mHeaderLayout == null ? 0 : 1;
}

/**
* if mFooterLayout is null will be return false or not will be true
*
* @return
* if addFooterView will be return 1, if not will be return 0
*/
public boolean hasFooterLayout() {
return mFooterLayout != null;
public int getFooterLayoutCount() {
return mFooterLayout == null ? 0 : 1;
}

/**
Expand All @@ -433,7 +433,7 @@ public int getmEmptyViewCount() {
@Override
public int getItemCount() {
int i = isLoadMore() ? 1 : 0;
int count = mData.size() + i + getHeaderViewsCount() + getFooterViewsCount();
int count = mData.size() + i + getHeaderLayoutCount() + getFooterLayoutCount();
if (mData.size() == 0 && mEmptyView != null) {
/**
* setEmptyView(false) and add emptyView
Expand All @@ -447,7 +447,7 @@ public int getItemCount() {
count += getmEmptyViewCount();
}

if ((mHeadAndEmptyEnable && getHeaderViewsCount() == 1 && count == 1) || count == 0) {
if ((mHeadAndEmptyEnable && getHeaderLayoutCount() == 1 && count == 1) || count == 0) {
mEmptyEnable = true;
count += getmEmptyViewCount();
}
Expand Down Expand Up @@ -514,13 +514,13 @@ else if ((!mFootAndEmptyEnable || !mHeadAndEmptyEnable) && position == 1 && mFoo
}
} else if (mData.size() == 0 && mEmptyView != null && getItemCount() == (mHeadAndEmptyEnable ? 2 : 1) && mEmptyEnable) {
return EMPTY_VIEW;
} else if (position == mData.size() + getHeaderViewsCount()) {
} else if (position == mData.size() + getHeaderLayoutCount()) {
if (mNextLoadEnable)
return LOADING_VIEW;
else
return FOOTER_VIEW;
}
return getDefItemViewType(position - getHeaderViewsCount());
return getDefItemViewType(position - getHeaderLayoutCount());
}

protected int getDefItemViewType(int position) {
Expand Down Expand Up @@ -621,7 +621,7 @@ public void onBindViewHolder(final RecyclerView.ViewHolder holder, int positions

switch (viewType) {
case 0:
convert((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderViewsCount()));
convert((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderLayoutCount()));
addAnimation(holder);
break;
case LOADING_VIEW:
Expand All @@ -634,8 +634,8 @@ public void onBindViewHolder(final RecyclerView.ViewHolder holder, int positions
case FOOTER_VIEW:
break;
default:
convert((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderViewsCount()));
onBindDefViewHolder((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderViewsCount()));
convert((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderLayoutCount()));
onBindDefViewHolder((BaseViewHolder) holder, mData.get(holder.getLayoutPosition() - getHeaderLayoutCount()));
break;
}

Expand All @@ -652,6 +652,20 @@ protected BaseViewHolder createBaseViewHolder(ViewGroup parent, int layoutResId)
return new BaseViewHolder(mContentView);
}

/**
* Return root layout of header
*/
public LinearLayout getHeaderLayout() {
return mHeaderLayout;
}

/**
* Return root layout of footer
*/
public LinearLayout getFooterLayout() {
return mFooterLayout;
}

/**
* Append header to the rear of the mHeaderLayout.
*
Expand All @@ -673,9 +687,14 @@ public void addHeaderView(View header) {
*/
public void addHeaderView(View header, int index) {
if (mHeaderLayout == null) {
mHeaderLayout = new LinearLayout(header.getContext());
mHeaderLayout.setOrientation(LinearLayout.VERTICAL);
mHeaderLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
if (mCopyHeaderLayout == null) {
mHeaderLayout = new LinearLayout(header.getContext());
mHeaderLayout.setOrientation(LinearLayout.VERTICAL);
mHeaderLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
mCopyHeaderLayout = mHeaderLayout;
} else {
mHeaderLayout = mCopyHeaderLayout;
}
}
index = index >= mHeaderLayout.getChildCount() ? -1 : index;
mHeaderLayout.addView(header, index);
Expand Down Expand Up @@ -704,9 +723,14 @@ public void addFooterView(View footer) {
public void addFooterView(View footer, int index) {
mNextLoadEnable = false;
if (mFooterLayout == null) {
mFooterLayout = new LinearLayout(footer.getContext());
mFooterLayout.setOrientation(LinearLayout.VERTICAL);
mFooterLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
if (mCopyFooterLayout == null) {
mFooterLayout = new LinearLayout(footer.getContext());
mFooterLayout.setOrientation(LinearLayout.VERTICAL);
mFooterLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
mCopyFooterLayout = mFooterLayout;
} else {
mFooterLayout = mCopyFooterLayout;
}
}
index = index >= mFooterLayout.getChildCount() ? -1 : index;
mFooterLayout.addView(footer, index);
Expand Down Expand Up @@ -859,15 +883,15 @@ private void initItemClickListener(final BaseViewHolder baseViewHolder) {
baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRecyclerViewItemClickListener.onItemClick(v, baseViewHolder.getLayoutPosition() - getHeaderViewsCount());
onRecyclerViewItemClickListener.onItemClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
}
});
}
if (onRecyclerViewItemLongClickListener != null) {
baseViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return onRecyclerViewItemLongClickListener.onItemLongClick(v, baseViewHolder.getLayoutPosition() - getHeaderViewsCount());
return onRecyclerViewItemLongClickListener.onItemLongClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
}
});
}
Expand Down

0 comments on commit 1a32778

Please sign in to comment.