RecyclerView 本身是不支持添加底部和头部的,可以对其进行功能扩展,使其能够支持底部和头部的添加:
package com.retrofit.retrofitmaster.ui.view.adapter; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; /** * author : mjj * create : 2017/10/18:9:09 */ public class WrapRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { // 包装 adapter 是原来的 RecyclerView.Adapter 是并不支持添加头部和底部的 private RecyclerView.Adapter mRealAdapter; ArrayList<View> mHeaderViews; // 头部 ArrayList<View> mFooterViews; // 底部 public WrapRecyclerAdapter(RecyclerView.Adapter adapter) { mRealAdapter = adapter; mHeaderViews = new ArrayList<>(); mFooterViews = new ArrayList<>(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) { // Header (negative positions will throw an IndexOutOfBoundsException) int numHeaders = getHeadersCount(); if (position < numHeaders) { return createFooterHeaderViewHolder(mHeaderViews.get(position)); } // Adapter final int adjPosition = position - numHeaders; int adapterCount = 0; if (mRealAdapter != null) { adapterCount = mRealAdapter.getItemCount(); if (adjPosition < adapterCount) { return mRealAdapter.onCreateViewHolder(parent, mRealAdapter.getItemViewType(adjPosition)); } } // Footer (off-limits positions will throw an IndexOutOfBoundsException) return createFooterHeaderViewHolder(mFooterViews.get(adjPosition - adapterCount)); } public int getHeadersCount() { return mHeaderViews.size(); } private RecyclerView.ViewHolder createFooterHeaderViewHolder(View view) { return new RecyclerView.ViewHolder(view) { }; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { // Header (negative positions will throw an IndexOutOfBoundsException) int numHeaders = getHeadersCount(); if (position < numHeaders) { return; } // Adapter final int adjPosition = position - numHeaders; if (mRealAdapter != null) { int adapterCount = mRealAdapter.getItemCount(); if (adjPosition < adapterCount) { mRealAdapter.onBindViewHolder(holder, adjPosition); } } } @Override public int getItemViewType(int position) { return position; } /** * 添加底部View * @param view */ public void addFooterView(View view) { if (!mFooterViews.contains(view)) { mFooterViews.add(view); notifyDataSetChanged(); } } /** * 添加头部View * @param view */ public void addHeaderView(View view) { if (!mHeaderViews.contains(view)) { mHeaderViews.add(view); notifyDataSetChanged(); } } /** * 移除底部View * @param view */ public void removeFooterView(View view) { if (mFooterViews.contains(view)) { mFooterViews.remove(view); notifyDataSetChanged(); } } /** * 移除头部View * @param view */ public void removeHeaderView(View view) { if (mHeaderViews.contains(view)) { mHeaderViews.remove(view); notifyDataSetChanged(); } } @Override public int getItemCount() { return mRealAdapter.getItemCount() + mHeaderViews.size() + mFooterViews.size(); } }
package com.retrofit.retrofitmaster.ui.view.adapter; import android.content.Context; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; /** * author : mjj * create : 2017/10/18:10:22 */ public class WrapRecyclerView extends RecyclerView { // 支持添加头部和底部的 RecyclerView.Adapter private WrapRecyclerAdapter mWrapAdapter; public WrapRecyclerView(Context context) { super(context); } public WrapRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public WrapRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setAdapter(Adapter adapter) { // 这里做一个替换 mWrapAdapter = new WrapRecyclerAdapter(adapter); super.setAdapter(mWrapAdapter); } /** * 添加头部View * @param view */ public void addHeaderView(View view){ if(mWrapAdapter != null){ mWrapAdapter.addHeaderView(view); } } /** * 添加底部View * @param view */ public void addFooterView(View view){ if(mWrapAdapter != null){ mWrapAdapter.addFooterView(view); } } /** * 移除头部View * @param view */ public void removeHeaderView(View view){ if(mWrapAdapter != null){ mWrapAdapter.removeHeaderView(view); } } /** * 移除底部View * @param view */ public void removeFooterView(View view){ if(mWrapAdapter != null){ mWrapAdapter.removeFooterView(view); } } }
使用
//设置manager wrapRv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); // 实例化头部View View headerView = LayoutInflater.from(this).inflate(R.layout.head_layout, wrapRv, false); // 设置适配器 wrapRv.setAdapter(new RecyclerViewAdapter(this,datas)); // 添加头部 wrapRv.addHeaderView(headerView);
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦