Recycleview封装Adapter增加头部尾部

架构师 来源:吃故事的妖怪 1672℃ 0评论

这次封装了recycleview的adapter。众所周知,recycleview可塑性很强,可是谷歌爸爸把许多listview常用的功能给去掉了,让我们自己封装。今天,终于回过神来搞定了recycleview的头部添加方法,为了以后使用方便,变成依赖形式方便大家使用。
使用方法:


主路径的build文件里下添加maven{url'https://jitpack.io'},项目的build文件里添加
compile'com.github.Rainorsad:RecycleItemOrition:v3.0'
好,我们转身看一下使用方法:


是不是和原生的Recycleview.Adapter很像,其实就是很想,封装后要的就是简洁明了,大部分封装代码在下面说,接下来在adapter中调用的话就是


前两行是设置adapter,后两行是设置头部view。注意,在设置头部view这里,如果你的头部view就是一个静态界面不需要交互的话,那么直解用
adapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.xxx,null))
的方式就好,如果涉及到交互的话,那么你需要重新自定义写HeadView,简单的写法如下所示:


用法讲解完了,我们一起来看看内部到底怎么封装的代码。

public abstract class HFViewAdapter extends RecyclerView.Adapter{

    private Context c;
    private List data;
    private RecyclerView mRecyclerView;
    private int TYPE_HEADVIEW = 1001;
    private int TYPE_FOOTVIEW = 2002;
    private int TYPE_NORMAL = 3003;
    private View headView;
    private View footView;

    protected HFViewAdapter(Context c,List s) {
        this.c = c;
        this.data = s;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADVIEW){
            return new MyViewHolder(headView);
        }else if (viewType == TYPE_FOOTVIEW){
            return new MyViewHolder(footView);
        }else {
            return onCreateViewHolde(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (!isHeaderView(position) && !isFooterView(position)){
            if (haveHeaderView()) position--;
            onBindViewHolde(holder,position);
        }
    }

    @Override
    public int getItemCount() {
        int count = getItemCounts();
        if (footView != null) {
            count++;
        }
        if (headView != null) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)){
            return TYPE_HEADVIEW;
        }else if (isFooterView(position)){
            return TYPE_FOOTVIEW;
        }else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addHeaderView(View headerView) {
        if (haveHeaderView()) {
            throw new IllegalStateException("hearview has already exists!");
        } else {
            //避免出现宽度自适应
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(params);
            headView = headerView;
            ifGridLayoutManager();
            notifyItemInserted(0);
        }
    }

    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footerView.setLayoutParams(params);
            footView = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }

    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup();
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isHeaderView(position) || isFooterView(position)) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1;
                }
            });
        }
    }

    protected abstract RecyclerView.ViewHolder onCreateViewHolde(ViewGroup parent, int viewType);

    protected abstract void onBindViewHolde(RecyclerView.ViewHolder holder, int position);

    protected abstract int getItemCounts();

    private boolean haveHeaderView() {
        return headView != null;
    }

    public boolean haveFooterView() {
        return footView != null;
    }

    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }

    private boolean isFooterView(int position) {
        return haveFooterView() && position == getItemCount() - 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }
}

其实就是简单的对viewtype的判断封装,然后对itemCount的处理,大家方便使用就好。
项目地址:https://github.com/Rainorsad/RecycleItemOrition

关闭

IT问道推荐

银行贷款频频被拒?
“Dr信用牛牛”让你远离信用污点 国内首家信用健康管理平台免费为你提供信用修复方案