public class LRecyclerView extends RecyclerView
{
private Adapter mAdapter;
//定义数据
public class FixedViewInfo
{
public View view;
public Object data;
public boolean isSelectable;
}
private ArrayList mHeaderViewInfos = new ArrayList<>();
private ArrayList mFooterViewInfos = new ArrayList<>();
public LRecyclerView(Context context)
{
this(context, null);
}
public LRecyclerView(Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
}
//添加头部
public void addHeaderView(View v, Object data, boolean isSelectable)
{
final FixedViewInfo info = new FixedViewInfo();
info.view = v;
info.data = data;
info.isSelectable = isSelectable;
mHeaderViewInfos.add(info);
}
public void addHeaderView(View v)
{
addHeaderView(v, null, true);
}
//获取头部数量
public int getHeaderViewCount()
{
return mHeaderViewInfos.size();
}
//移除头部
public boolean removeHeaderView(View v)
{
if (mHeaderViewInfos.size() > 0)
{
boolean result = false;
if (mAdapter != null && ((WrapAdapter) mAdapter).removeHeaderView(v))
{
result = true;
}
//从mHeaderViewInfos移除数据
removeFixedViewInfo(v, mHeaderViewInfos);
return result;
}
return false;
}
//移除list中的数据
private void removeFixedViewInfo(View v, ArrayList where)
{
int len = where.size();
for (int i = 0; i < len; ++i)
{
FixedViewInfo info = where.get(i);
if (info.view == v)
{
where.remove(i);
break;
}
}
}
//添加尾部
public void addFooterView(View v, Object data, boolean isSelectable)
{
final FixedViewInfo info = new FixedViewInfo();
info.view = v;
info.data = data;
info.isSelectable = isSelectable;
mFooterViewInfos.add(info);
}
public void addFooterView(View v)
{
addFooterView(v, null, true);
}
//移除尾部
public boolean removeFooterView(View v)
{
//判断有尾部,则移除
if (mFooterViewInfos.size() > 0)
{
boolean result = false;
if (mAdapter != null && ((WrapAdapter) mAdapter).removeFooterView(v))
{
result = true;
}
//从mFooterViewInfos移除数据
removeFixedViewInfo(v, mFooterViewInfos);
return result;
}
return false;
}
@Override
public void setAdapter(Adapter adapter)
{
//判断有头部或尾部,mAdapter为WrapAdapter,否则为adapter
if (mHeaderViewInfos.size() > 0 || mFooterViewInfos.size() > 0)
{
mAdapter = new WrapAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
} else
{
mAdapter = adapter;
}
super.setAdapter(mAdapter);
}
}
自定义的WrapAdapter,参照ListView的WrapAdapter
public class WrapAdapter extends RecyclerView.Adapter {
public static final int ITEM_VIEW_TYPE_HEADER = -2;
public static final int ITEM_VIEW_TYPE_FOOTER = -3;
private int headerPosition = 0;
private int footerPosition = 0;
private final RecyclerView.Adapter mAdapter;
ArrayList mHeaderViewInfos;
ArrayList mFooterViewInfos;
static final ArrayList EMPTY_INFO_LIST = new ArrayList<>();
public WrapAdapter(ArrayList headerViewInfos,
ArrayList footerViewInfos,
RecyclerView.Adapter adapter) {
mAdapter = adapter;
if (headerViewInfos == null) {
mHeaderViewInfos = EMPTY_INFO_LIST;
} else {
mHeaderViewInfos = headerViewInfos;
}
if (footerViewInfos == null) {
mFooterViewInfos = EMPTY_INFO_LIST;
} else {
mFooterViewInfos = footerViewInfos;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_VIEW_TYPE_HEADER) {
return new ViewHolder(mHeaderViewInfos.get(headerPosition++).view);
} else if (viewType == ITEM_VIEW_TYPE_FOOTER) {
return new ViewHolder(mFooterViewInfos.get(footerPosition++).view);
} else {
return mAdapter.onCreateViewHolder(parent, viewType);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int numHeaders = getHeaderViewCount();
if (position < numHeaders) {
return;
}
final int adjPosition = position - numHeaders;
int adapterCount = 0;
if (mAdapter != null) {
adapterCount = mAdapter.getItemCount();
if (adjPosition < adapterCount) {
mAdapter.onBindViewHolder(holder, position-numHeaders);
}
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
int numHeaders = getHeaderViewCount();
if (holder.getLayoutPosition() < numHeaders) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(true);
}
final int adjPosition = holder.getLayoutPosition() - numHeaders;
int adapterCount = 0;
adapterCount = mAdapter.getItemCount();
if (adjPosition < adapterCount) {
return;
}
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(true);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if(manager instanceof GridLayoutManager) {
final GridLayoutManager gridManager = ((GridLayoutManager) manager);
gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int numHeaders = getHeaderViewCount();
if (position < numHeaders) {
return gridManager.getSpanCount();
}
final int adjPosition = position - numHeaders;
int adapterCount = 0;
adapterCount = mAdapter.getItemCount();
if (adjPosition < adapterCount) {
return 1;
}
return gridManager.getSpanCount();
}
});
}
}
@Override
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
if (mAdapter != null)
mAdapter.registerAdapterDataObserver(observer);
}
@Override
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
if (mAdapter != null)
mAdapter.unregisterAdapterDataObserver(observer);
}
@Override
public int getItemCount() {
if (mAdapter != null) {
return mAdapter.getItemCount() + getHeaderViewCount() + getFooterViewCount();
}
return getHeaderViewCount() + getFooterViewCount();
}
public int getHeaderViewCount() {
return mHeaderViewInfos.size();
}
public int getFooterViewCount() {
return mFooterViewInfos.size();
}
@Override
public int getItemViewType(int position) {
int numHeaders = getHeaderViewCount();
if (position < numHeaders) {
return ITEM_VIEW_TYPE_HEADER;
}
if (mAdapter != null && position >= numHeaders) {
int adjPosition = position - numHeaders;
int adapterCount = mAdapter.getItemCount();
if (adjPosition < adapterCount) {
return mAdapter.getItemViewType(adjPosition);
}
}
return ITEM_VIEW_TYPE_FOOTER;
}
@Override
public long getItemId(int position) {
int numHeaders = getHeaderViewCount();
if (mAdapter != null && position >= numHeaders) {
int adjPosition = position - numHeaders;
int adapterCount = mAdapter.getItemCount();
if (adjPosition < adapterCount) {
return mAdapter.getItemId(adjPosition);
}
}
return -1;
}
public boolean removeHeaderView(View v) {
for (int i = 0; i < mHeaderViewInfos.size(); i++) {
LRecyclerView.FixedViewInfo info = mHeaderViewInfos.get(i);
if (info.view == v) {
mHeaderViewInfos.remove(i);
return true;
}
}
return false;
}
public boolean removeFooterView(View v) {
for (int i = 0; i < mFooterViewInfos.size(); i++) {
LRecyclerView.FixedViewInfo info = mFooterViewInfos.get(i);
if (info.view == v) {
mFooterViewInfos.remove(i);
return true;
}
}
return false;
}
private static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}