package com.jingchen.pulltorefresh;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class RecyclerWrapAdapter extends RecyclerView.Adapter implements WrapperAdapter
{

    private RecyclerView.Adapter mAdapter;

    private ArrayList<View> mHeaderViews;

    private ArrayList<View> mFootViews;

    static final ArrayList<View> EMPTY_INFO_LIST =
            new ArrayList<View>();

    private int mCurrentPosition;

    public RecyclerWrapAdapter(ArrayList<View> mHeaderViews, ArrayList<View> mFootViews, RecyclerView.Adapter mAdapter)
    {
        this.mAdapter = mAdapter;
        if (mHeaderViews == null)
        {
            this.mHeaderViews = EMPTY_INFO_LIST;
        } else
        {
            this.mHeaderViews = mHeaderViews;
        }
        if (mHeaderViews == null)
        {
            this.mFootViews = EMPTY_INFO_LIST;
        } else
        {
            this.mFootViews = mFootViews;
        }
    }

    public int getHeadersCount()
    {
        return mHeaderViews.size();
    }

    public int getFootersCount()
    {
        return mFootViews.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == RecyclerView.INVALID_TYPE)
        {
            mHeaderViews.get(0).setLayoutParams(parent.getLayoutParams());
            return new HeaderViewHolder(mHeaderViews.get(0));
        } else if (viewType == RecyclerView.INVALID_TYPE - 1)
        {
            mFootViews.get(0).setLayoutParams(parent.getLayoutParams());
            return new HeaderViewHolder(mFootViews.get(0));
        }
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        int numHeaders = getHeadersCount();
        if (position < numHeaders)
        {
            return;
        }
        int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null)
        {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount)
            {
                mAdapter.onBindViewHolder(holder, adjPosition);
                return;
            }
        }
    }

    @Override
    public int getItemCount()
    {
        if (mAdapter != null)
        {
            return getHeadersCount() + getFootersCount() + mAdapter.getItemCount();
        } else
        {
            return getHeadersCount() + getFootersCount();
        }
    }

    @Override
    public int getItemViewType(int position)
    {
        mCurrentPosition = position;
        int numHeaders = getHeadersCount();
        if (position < numHeaders)
        {
            return RecyclerView.INVALID_TYPE;
        }
        int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null)
        {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount)
            {
                return mAdapter.getItemViewType(adjPosition);
            }
        }
        return RecyclerView.INVALID_TYPE - 1;
    }


    @Override
    public long getItemId(int position)
    {
        int numHeaders = getHeadersCount();
        if (mAdapter != null && position >= numHeaders)
        {
            int adjPosition = position - numHeaders;
            int adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount)
            {
                return mAdapter.getItemId(adjPosition);
            }
        }
        return -1;
    }


    @Override
    public RecyclerView.Adapter getWrappedAdapter()
    {
        return mAdapter;
    }

    private static class HeaderViewHolder extends RecyclerView.ViewHolder
    {
        public HeaderViewHolder(View itemView)
        {
            super(itemView);
        }
    }
}

@@ -464,52 +464,14 @@
 ist%3C
-View
 %3E();%0A%0A
-    private int mCurrentPosition;%0A
 %0A   
@@ -506,34 +506,33 @@
 ArrayList%3CView%3E 
-mH
+h
 eaderViews, Arra
@@ -535,34 +535,33 @@
 ArrayList%3CView%3E 
-mF
+f
 ootViews, Recycl
@@ -571,26 +571,25 @@
 iew.Adapter 
-mA
+a
 dapter)%0A    
@@ -598,21 +598,16 @@
         
-this.
 mAdapter
@@ -609,18 +609,17 @@
 apter = 
-mA
+a
 dapter;%0A
@@ -622,32 +622,40 @@
 er;%0A        if (
+null == 
 mHeaderViews == 
@@ -646,32 +646,24 @@
 mHeaderViews
- == null
 )%0A        %7B%0A
@@ -666,37 +666,32 @@
   %7B%0A            
-this.
 mHeaderViews = E
@@ -743,21 +743,16 @@
         
-this.
 mHeaderV
@@ -758,18 +758,17 @@
 Views = 
-mH
+h
 eaderVie
@@ -797,28 +797,26 @@
 if (
-mHeaderViews == null
+null == mFootViews
 )%0A  
@@ -827,37 +827,32 @@
   %7B%0A            
-this.
 mFootViews = EMP
@@ -906,13 +906,8 @@
     
-this.
 mFoo
@@ -915,18 +915,17 @@
 Views = 
-mF
+f
 ootViews
@@ -2602,45 +2602,8 @@
   %7B%0A
-        mCurrentPosition = position;%0A
     

修复了RecyclerWrapAdapter 里面的FootView的null判断错误