内部有一个WrapAdapter,我们传入的adapter会在这个WrapAdapter里面
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_REFRESH_HEADER) {
// TODO: 2020/11/18 6:25 PM chenyanping onCreateViewHolder 如果我设置pullRefreshEnabled为false,mRefreshHeader为null,会异常吗
return new SimpleViewHolder(mRefreshHeader);
} else if (isHeaderType(viewType)) {
return new SimpleViewHolder(getHeaderViewByType(viewType));
} else if (viewType == TYPE_FOOTER) {
return new SimpleViewHolder(mFootView);
}
return adapter.onCreateViewHolder(parent, viewType);
}
@Override
public int getItemViewType(int position) {
int adjPosition = position - (getHeadersCount() + 1);
if (isRefreshHeader(position)) {
return TYPE_REFRESH_HEADER;
}
if (isHeader(position)) {
position = position - 1;
return sHeaderTypes.get(position);
}
if (isFooter(position)) {
return TYPE_FOOTER;
}
int adapterCount;
if (adapter != null) {
adapterCount = adapter.getItemCount();
if (adjPosition < adapterCount) {
int type = adapter.getItemViewType(adjPosition);
if(isReservedItemViewType(type)) {
throw new IllegalStateException("XRecyclerView require itemViewType in adapter should be less than 10000 " );
}
return type;
}
}
return 0;
}
默认第一个item就是刷新头,所以外面用XrecyclerView时想刷新pos位置的item,需要设置pos+getHeadersCount() + 1;其中1是XrecyclerView的刷新头,getHeadersCount()是你加入的headview的个数
public boolean isRefreshHeader(int position) {
return position == 0;
}
public boolean isHeader(int position) {
return position >= 1 && position < mHeaderViews.size() + 1;
}
public int getHeadersCount() {
return mHeaderViews.size();
}
网友评论