美文网首页
Recycleview封装Adapter增加头部尾部

Recycleview封装Adapter增加头部尾部

作者: 石榴的咩唁 | 来源:发表于2017-08-29 17:09 被阅读0次

    这次封装了recycleview的adapter。众所周知,recycleview可塑性很强,可是谷歌爸爸把许多listview常用的功能给去掉了,让我们自己封装。之前就遇到过很尴尬的事,有个情景是需要给你自己的adapter添加个头部布局,但是发现竟然没有!!由于项目紧急,只好先用listview来代替recycleview。今天,终于回过神来搞定了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 classHFViewAdapterextendsRecyclerView.Adapter{

    privateContextc;

    privateListdata;

    privateRecyclerViewmRecyclerView;

    private intTYPE_HEADVIEW=1001;

    private intTYPE_FOOTVIEW=2002;

    private intTYPE_NORMAL=3003;

    privateViewheadView;

    privateViewfootView;

    protectedHFViewAdapter(Context c,List s) {

    this.c= c;

    this.data= s;

    }

    @Override

    publicRecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {

    if(viewType ==TYPE_HEADVIEW){

    return newMyViewHolder(headView);

    }else if(viewType ==TYPE_FOOTVIEW){

    return newMyViewHolder(footView);

    }else{

    returnonCreateViewHolde(parent,viewType);

    }

    }

    @Override

    public voidonBindViewHolder(RecyclerView.ViewHolder holder, intposition) {

    if(!isHeaderView(position) && !isFooterView(position)){

    if(haveHeaderView()) position--;

    onBindViewHolde(holder,position);

    }

    }

    @Override

    public intgetItemCount() {

    intcount = getItemCounts();

    if(footView!=null) {

    count++;

    }

    if(headView!=null) {

    count++;

    }

    returncount;

    }

    @Override

    public intgetItemViewType(intposition) {

    if(isHeaderView(position)){

    returnTYPE_HEADVIEW;

    }else if(isFooterView(position)){

    returnTYPE_FOOTVIEW;

    }else{

    returnTYPE_NORMAL;

    }

    }

    @Override

    public voidonAttachedToRecyclerView(RecyclerView recyclerView) {

    try{

    if(mRecyclerView==null&&mRecyclerView!= recyclerView) {

    mRecyclerView= recyclerView;

    }

    ifGridLayoutManager();

    }catch(Exception e) {

    e.printStackTrace();

    }

    }

    public voidaddHeaderView(View headerView) {

    if(haveHeaderView()) {

    throw newIllegalStateException("hearview has already exists!");

    }else{

    //避免出现宽度自适应

    ViewGroup.LayoutParams params =newViewGroup.LayoutParams

    (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

    headerView.setLayoutParams(params);

    headView= headerView;

    ifGridLayoutManager();

    notifyItemInserted(0);

    }

    }

    public voidaddFooterView(View footerView) {

    if(haveFooterView()) {

    throw newIllegalStateException("footerView has already exists!");

    }else{

    ViewGroup.LayoutParams params =newViewGroup.LayoutParams

    (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

    footerView.setLayoutParams(params);

    footView= footerView;

    ifGridLayoutManager();

    notifyItemInserted(getItemCount() -1);

    }

    }

    private voidifGridLayoutManager() {

    if(mRecyclerView==null)return;

    finalRecyclerView.LayoutManager layoutManager =mRecyclerView.getLayoutManager();

    if(layoutManagerinstanceofGridLayoutManager) {

    finalGridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup();

    ((GridLayoutManager) layoutManager).setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {

    @Override

    public intgetSpanSize(intposition) {

    return(isHeaderView(position) || isFooterView(position)) ? ((GridLayoutManager)layoutManager).getSpanCount() :1;

    }

    });

    }

    }

    protected abstractRecyclerView.ViewHolderonCreateViewHolde(ViewGroup parent, intviewType);

    protected abstract voidonBindViewHolde(RecyclerView.ViewHolder holder, intposition);

    protected abstract intgetItemCounts();

    private booleanhaveHeaderView() {

    returnheadView!=null;

    }

    public booleanhaveFooterView() {

    returnfootView!=null;

    }

    private booleanisHeaderView(intposition) {

    returnhaveHeaderView() && position ==0;

    }

    private booleanisFooterView(intposition) {

    returnhaveFooterView() && position == getItemCount() -1;

    }

    classMyViewHolderextendsRecyclerView.ViewHolder{

    publicMyViewHolder(View itemView) {

    super(itemView);

    }

    }

    }

    ```



    相关文章

      网友评论

          本文标题:Recycleview封装Adapter增加头部尾部

          本文链接:https://www.haomeiwen.com/subject/enzodxtx.html