package com.marshalchen.ultimaterecyclerview;

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

import java.util.Collections;
import java.util.List;

/**
 * Created by cym on 15-1-20.
 */
public abstract class UltimateViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.bottom_progressbar, parent, false);
        if (viewType == 1) return new ProgressBarViewHolder(v);
        return onCreateViewHolder(parent);

    }

    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent);

    /**
     * Return the view type of the item at <code>position</code> for the purposes
     * of view recycling.
     * <p/>
     * <p>The default implementation of this method returns 0, making the assumption of
     * a single view type for the adapter. Unlike ListView adapters, types need not
     * be contiguous. Consider using id resources to uniquely identify item view types.
     *
     * @param position position to query
     * @return integer value identifying the type of the view needed to represent the item at
     * <code>position</code>. Type codes need not be contiguous.
     */
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return 1;
        } else
            return super.getItemViewType(position);
    }


    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return getAdapterItemCount() + 1;
    }

    /**
     * Returns the number of items in the adapter bound to the parent RecyclerView.
     *
     * @return The number of items in the bound adapter
     */
    public abstract int getAdapterItemCount();

//    public void insertItem(int position) {
//        insert(position);
//        notifyItemInserted(position);
//    }
//
//    public abstract void insert(int position);


    public void toggleSelection(int pos) {
        notifyItemChanged(pos);
    }


    public void clearSelection(int pos) {
        notifyItemChanged(pos);
    }

    public void setSelected(int pos) {
        notifyItemChanged(pos);
    }



    public void swapPositions(List<?> list, int from, int to) {
        Collections.swap(list, from, to);
    }

    public <T> void insert(List<T> list, T object, int position) {
        list.add(position, object);
        notifyItemInserted(position);
        notifyItemChanged(position + 1);
    }

    public void remove(List<?> list,int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    public void clear(List<?> list) {
        int size = list.size();
        list.clear();
        notifyItemRangeRemoved(0, size);
    }

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

    }


}

@@ -372,16 +372,61 @@
 der%3E %7B%0A%0A
+    private View customLoadMoreView = null;%0A%0A
     @Ove
@@ -515,24 +515,182 @@
 viewType) %7B%0A
+        if (viewType == 1) %7B%0A            Logs.d(%22custom load more view   %22+(customLoadMoreView==null));%0A            if (customLoadMoreView == null) %7B%0A        
         View
@@ -751,16 +751,24 @@
         
+      
+  
 .inflate
@@ -821,34 +821,23 @@
         
-if (viewType == 1)
+       
  return 
@@ -878,736 +878,344 @@
     
-return onCreateViewHolder(parent);%0A%0A    %7D%0A%0A    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent);%0A%0A    /**%0A
+    %7D else %7B%0A          
      
-* R
+ r
 eturn 
-the view type of the item at %3Ccode%3Eposition%3C/code%3E for the purposes%0A     * of view recycling.%0A     * %3Cp/%3E%0A     * %3Cp%3EThe default implementation of this method returns 0, making the assumption of%0A     * a single view type for the adapter. Unlike ListView adapters, types need not%0A     * be contiguous. Consider using id resources to uniquely identify item view types.
+new ProgressBarViewHolder(customLoadMoreView);%0A            %7D%0A        %7D%0A%0A%0A        return onCreateViewHolder(parent);%0A%0A    %7D%0A%0A    public void setCustomLoadMoreView(View view) %7B
 %0A     
-*%0A
    
-  * @param position position to query%0A     * @return integer value identifying the type of the view needed to represent the item at%0A     * %3Ccode%3Eposition%3C/code%3E. Type codes need not be contiguous.%0A     */
+customLoadMoreView = view;%0A    %7D%0A%0A    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent);%0A%0A
 %0A   
@@ -2298,17 +2298,16 @@
     %7D%0A%0A%0A
-%0A
     publ
@@ -2636,16 +2636,17 @@
 ?%3E list,
+ 
 int posi

update UltimateViewAdapter for custom loading view