添加Footer的流程
1.getItemViewType—2.onCreateViewHolder—3.onBindViewHolder—4.getItemCount
对于是否加载footer的标志,应该在recyclerview滑动的时候来判断,所以传一个adapter。
public abstract class BaseListActivity<T> extends BaseActivity implements PullRecycler.onRecyclerRefreshListener {
protected PullRecycler recycler;
protected BaseListAdapter adapter;
protected ArrayList<T> mDataList;
@Override
protected void setUpContentView() {
setContentView(R.layout.activity_sample_list, R.string.sample_list_title);
}
@Override
protected void setUpView() {
recycler = (PullRecycler) findViewById(R.id.pullToRefreshRecycler);
}
@Override
protected void setUpData() {
adapter = new BaseListAdapter();
recycler.setOnRefreshListener(this);
recycler.setLayoutManager(getLayoutManager());
recycler.addItemDecoration(getItemDecoration());
recycler.setAdapter(adapter);
}
protected RecyclerView.ItemDecoration getItemDecoration() {
return new DividerItemDecoration(getApplicationContext(), R.drawable.list_divider);
}
protected ILayoutManager getLayoutManager() {
return new MyLinearLayoutManager(getApplicationContext());
}
class BaseListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private static final int VIE_TYPE_LOAD_MORE_FOOTER = 100;
private boolean isShowLoadMoreFooter;
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIE_TYPE_LOAD_MORE_FOOTER) {
return getLoadMoreFooter(parent);//加载不同布局
}
return getViewHolder(parent, viewType);
}
private BaseViewHolder getLoadMoreFooter(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.widget_pull_to_refresh_footer, parent, false);
return new LoadMoreFooterViewHolder(view);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
//先判断是不是瀑布流,让footer独自占一行setFullSpan(true);
if (isShowLoadMoreFooter && position == getItemCount() - 1) {
if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
params.setFullSpan(true);
}
}
holder.onBindViewHolder(position);
}
@Override
public int getItemCount() {
return mDataList != null ? mDataList.size() + (isShowLoadMoreFooter ? 1 : 0) : 0;
}
@Override
public int getItemViewType(int position) {
if (isShowLoadMoreFooter && position == getItemCount() - 1) {
return VIE_TYPE_LOAD_MORE_FOOTER;
}
return getItemType(position);
}
public void showLoadMoreFooter(boolean isShow) {
this.isShowLoadMoreFooter = isShow;
if (isShow) {
notifyItemInserted(getItemCount());
} else {
notifyItemRemoved(getItemCount());
}
}
public boolean isFooterView(int position) {
return isShowLoadMoreFooter && position == getItemCount() - 1;
}
private class LoadMoreFooterViewHolder extends BaseViewHolder {
public LoadMoreFooterViewHolder(View view) {
super(view);
}
@Override
public void onBindViewHolder(int position) {
}
}
}
protected int getItemType(int position) {
return 0;
}
protected abstract BaseViewHolder getViewHolder(ViewGroup parent, int viewType);
}
对于GridLayoutManager中footer的处理
FooterSpanSizeLookup lookup = new FooterSpanSizeLookup(adapter, getSpanCount());//getCount先取到设置的值
setSpanSizeLookup(lookup);
public class FooterSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
private BaseListActivity.BaseListAdapter adapter;
private int spanCount;
public FooterSpanSizeLookup(BaseListActivity.BaseListAdapter adapter, int spanCount) {
this.adapter = adapter;
this.spanCount = spanCount;
}
//如果是footer,假设getCount是3,那么一个footer占3个位置,所以独自显示一行
//如果不是footer,默认是1
@Override
public int getSpanSize(int position) {
if (adapter.isFooterView(position)) {
return spanCount;
}
return 1;
}
}
public class MyStaggeredGridLayoutManager extends StaggeredGridLayoutManager implements ILayoutManager {
public MyStaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public MyStaggeredGridLayoutManager(int spanCount, int orientation) {
super(spanCount, orientation);
}
//判断最后一个
//还要判断是否独占一行
@Override
public int findLastVisiblePosition() {
int[] positions = null;
positions = findLastVisibleItemPositions(positions);
return positions[0];
}
@Override
public RecyclerView.LayoutManager getLayoutManager() {
return this;
}
@Override
public void setUpAdapter(BaseListActivity.BaseListAdapter adapter) {
}
}