package pabix.chickens.una;


import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import pabix.chickens.una.Item;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JunHyeok on 2016. 10. 17..
 */

public class ProjectRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private ArrayList<Item> itemList;

    private OnLoadMoreListener onLoadMoreListener;
    private LinearLayoutManager mLinearLayoutManager;

    private boolean isMoreLoading = false;
    private int visibleThreshold = 1;
    int firstVisibleItem, visibleItemCount, totalItemCount;

    public interface OnLoadMoreListener{
        void onLoadMore();
    }

    public ProjectRecyclerAdapter(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener=onLoadMoreListener;
        itemList =new ArrayList<>();
    }

    public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager){
        this.mLinearLayoutManager=linearLayoutManager;
    }

    public void setRecyclerView(RecyclerView mView){
        mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    visibleItemCount = recyclerView.getChildCount();
                    totalItemCount = mLinearLayoutManager.getItemCount();
                    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
                    if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
                        if (onLoadMoreListener != null) {
                            onLoadMoreListener.onLoadMore();
                        }
                        isMoreLoading = true;
                    }
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return itemList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_ITEM) {
            return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.project_list, parent, false));
        } else {
            return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
        }

    }

    public void addAll(List<Item> lst){
        itemList.clear();
        itemList.addAll(lst);
        notifyDataSetChanged();
    }

    public void addItemMore(List<Item> lst){
        itemList.addAll(lst);
        notifyItemRangeChanged(0,itemList.size());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StudentViewHolder) {
            Item singleItem = (Item) itemList.get(position);
            ((StudentViewHolder) holder).tvItem.setText(singleItem.getItem());
        }
    }

    public void setMoreLoading(boolean isMoreLoading) {
        this.isMoreLoading=isMoreLoading;
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public void setProgressMore(final boolean isProgress) {
        if (isProgress) {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    itemList.add(null);
                    notifyItemInserted(itemList.size() - 1);
                }
            });
        } else {
            itemList.remove(itemList.size() - 1);
            notifyItemRemoved(itemList.size());
        }
    }

    static class StudentViewHolder extends RecyclerView.ViewHolder {
        public TextView tvItem;

        public StudentViewHolder(View v) {
            super(v);
            tvItem = (TextView) v.findViewById(R.id.tvitem);
        }
    }

    static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar pBar;
        public ProgressViewHolder(View v) {
            super(v);
            pBar = (ProgressBar) v.findViewById(R.id.pBar);
        }
    }
}

@@ -147,24 +147,49 @@
 cyclerView;%0A
+import android.util.Log;%0A
 import andro
@@ -859,24 +859,25 @@
 hreshold = 1
+5
 ;%0A    int fi
@@ -1348,16 +1348,22 @@
 lerView(
+final 
 Recycler
@@ -1609,271 +1609,9 @@
 y);%0A
-            %7D%0A%0A            @Override%0A            public void onScrollStateChanged(RecyclerView recyclerView, int newState) %7B%0A                super.onScrollStateChanged(recyclerView, newState);%0A                if (newState == RecyclerView.SCROLL_STATE_IDLE) %7B%0A    
+%0A
     
@@ -1687,20 +1687,16 @@
         
-  
-  
 totalIte
@@ -1733,36 +1733,32 @@
 getItemCount();%0A
-    
                 
@@ -1825,28 +1825,25 @@
 Position();%0A
-    
+%0A
             
@@ -1963,28 +1963,24 @@
             
-    
 if (onLoadMo
@@ -1997,32 +1997,112 @@
  != null) %7B%0A    
+                    mView.onScrollStateChanged(RecyclerView.SCROLL_STATE_IDLE);%0A
                 
@@ -2142,32 +2142,88 @@
 More();%0A        
+            %7D%0A                    isMoreLoading = true;%0A
                 
@@ -2216,32 +2216,33 @@
               %7D%0A
+%0A
                 
@@ -2229,32 +2229,35 @@
  %7D%0A%0A            
+%7D%0A%0A
             isMo
@@ -2256,51 +2256,239 @@
     
-isMoreLoading = true;%0A                    %7D
+@Override%0A            public void onScrollStateChanged(RecyclerView recyclerView, int newState) %7B%0A                super.onScrollStateChanged(recyclerView, newState);%0A                if (newState == RecyclerView.SCROLL_STATE_IDLE) %7B
 %0A   
@@ -3098,25 +3098,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p

Remove Log