public class HeaderViewRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int HEADERS_START = Integer.MIN_VALUE;
private static final int FOOTERS_START = Integer.MIN_VALUE + 10;
private static final int ITEMS_START = Integer.MIN_VALUE + 20;
private static final int ADAPTER_MAX_TYPES = 100;
private RecyclerView.Adapter mWrappedAdapter;
private List<View> mHeaderViews, mFooterViews;
private Map<Class, Integer> mItemTypesOffset;
public HeaderViewRecyclerAdapter(RecyclerView.Adapter adapter) {
mHeaderViews = new ArrayList<>();
mFooterViews = new ArrayList<>();
mItemTypesOffset = new HashMap<>();
setWrappedAdapter(adapter);
}
public void setAdapter(RecyclerView.Adapter adapter) {
if (mWrappedAdapter != null && mWrappedAdapter.getItemCount() > 0) {
notifyItemRangeRemoved(getHeaderCount(), mWrappedAdapter.getItemCount());
}
setWrappedAdapter(adapter);
notifyItemRangeInserted(getHeaderCount(), mWrappedAdapter.getItemCount());
}
@Override
public int getItemViewType(int position) {
int hCount = getHeaderCount();
if (position < hCount) return HEADERS_START + position;
else {
int itemCount = mWrappedAdapter.getItemCount();
if (position < hCount + itemCount) {
return getAdapterTypeOffset() + mWrappedAdapter.getItemViewType(position - hCount);
} else return FOOTERS_START + position - hCount - itemCount;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType < HEADERS_START + getHeaderCount())
return new StaticViewHolder(mHeaderViews.get(viewType - HEADERS_START));
else if (viewType < FOOTERS_START + getFooterCount())
return new StaticViewHolder(mFooterViews.get(viewType - FOOTERS_START));
else {
return mWrappedAdapter.onCreateViewHolder(viewGroup, viewType - getAdapterTypeOffset());
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int hCount = getHeaderCount();
if (position >= hCount && position < hCount + mWrappedAdapter.getItemCount())
mWrappedAdapter.onBindViewHolder(viewHolder, position - hCount);
}
public void addHeaderView(View view) {
mHeaderViews.add(view);
}
public void addFooterView(View view) {
mFooterViews.add(view);
}
@Override
public int getItemCount() {
return getHeaderCount() + getFooterCount() + getWrappedItemCount();
}
public int getWrappedItemCount() {
return mWrappedAdapter.getItemCount();
}
public int getHeaderCount() {
return mHeaderViews.size();
}
public int getFooterCount() {
return mFooterViews.size();
}
private void setWrappedAdapter(RecyclerView.Adapter adapter) {
if (mWrappedAdapter != null) mWrappedAdapter.unregisterAdapterDataObserver(mDataObserver);
mWrappedAdapter = adapter;
Class adapterClass = mWrappedAdapter.getClass();
if (!mItemTypesOffset.containsKey(adapterClass)) putAdapterTypeOffset(adapterClass);
mWrappedAdapter.registerAdapterDataObserver(mDataObserver);
}
private void putAdapterTypeOffset(Class adapterClass) {
mItemTypesOffset.put(adapterClass, ITEMS_START + mItemTypesOffset.size() * ADAPTER_MAX_TYPES);
}
private int getAdapterTypeOffset() {
return mItemTypesOffset.get(mWrappedAdapter.getClass());
}
private RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
notifyDataSetChanged();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
notifyItemRangeChanged(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
notifyItemRangeInserted(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
notifyItemRangeRemoved(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
int hCount = getHeaderCount();
// TODO: No notifyItemRangeMoved method?
notifyItemRangeChanged(fromPosition + hCount, toPosition + hCount + itemCount);
}
};
private static class StaticViewHolder extends RecyclerView.ViewHolder {
public StaticViewHolder(View itemView) {
super(itemView);
}
}
@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 type = getItemViewType(position);
switch (type){
case FOOTERS_START:
return 2;
default:
return 1;
}
}
});
}
}
}
初始化FooterView
private void initView() {
firstCreateFootView = true;//首次加载headview,headview始终只能创建一次
manager = new LinearLayoutManager(getActivity());
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_fans.setLayoutManager(manager);
adapter = new FansInfoAdapter(getActivity());
/**
* 核心代码:RecyclerView.Adapter的adapter作为参数,初始化HeaderViewRecyclerAdapter
* headerViewRecyclerAdapter才是作为RecyclerView的adapter,也是在这一步完成绑定headerView
*/
headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(adapter);
recyclerView_fans.setAdapter(headerViewRecyclerAdapter);
recyclerView_fans.addOnScrollListener(new RecyclerView.OnScrollListener() {
boolean isSlidingToLast = false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//当newState为正在滚动状态时
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int lastVisiableItemPosition = linearLayoutManager.findLastVisibleItemPosition();
LogUtils.d("info", "[[[[[" + lastVisiableItemPosition + "-----------------" + footerAdapter.getItemCount());
if (lastVisiableItemPosition + 1 == footerAdapter.getItemCount() && footerAdapter.getItemCount() >= pageSize && isSlidingToLast) {
LogUtils.d("info", "滑动到底了pos:" + lastVisiableItemPosition + "-----------------" + footerAdapter.getItemCount());
//执行加载更多数据
simulateLoadMoreData();
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0)
isSlidingToLast = true;
else
isSlidingToLast = false;
}
});
}
核心的点在于何时去创建底部的headerview,这很有讲究,因为我们这里做的是滑动到底部自动加载更多数据,
因此判断这个临界点成为核心,解决思路:从每次的请求网络数据回调中判断数据条目是否满一页即是否等于
pageSize,瞒着条件即可调用创建hederview方法,动态添加headerview,注意:全程只可以创建一次,因此这
边还要控制好
//第一次网络请求,数据回调时进行判断,满足条件才创建
private void createLoadMoreView() {
if (getActivity() == null) {
return;
}
//headerView 布局可以自行定义
View loadMoreView = LayoutInflater.from(getActivity()).inflate(R.layout.item_view_load_more, recyclerView_fans, false);
headerViewRecyclerAdapter.addFooterView(loadMoreView);
}
内容全部加载完成后隐藏
//此处进行判断,满足条件时调用headerViewRecyclerAdapter.notifyItemRemoved(headerViewRecyclerAdapter.getItemCount());去掉headerview
if (null != fansCardTableBeen.get(0).getUser_phone() && fansCardTableBeen.get(0).getUser_phone().equals("")) {//已滑到最后一页
if (currentPage != 0) {
currentPage--;
ToastUtils.show(getActivity(), "没有更多数据");
headerViewRecyclerAdapter.notifyItemRemoved(headerViewRecyclerAdapter.getItemCount());
} else {
tv_empty_data.setVisibility(View.VISIBLE);
recyclerView_fans.setVisibility(View.GONE);
}
} else {
for (FansCardTableBean bean : fansCardTableBeen) {
list.add(bean);
}
adapter.setData(list);
adapter.notifyDataSetChanged();
}
}