package com.bustiblelemons.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

import com.bustiblelemons.bustiblelibs.R;

/**
 * Created by bhm on 19.07.14.
 */
public class LoadMoreViewPager extends ViewPager {

    public static final int LOAD_MORE_THRESHOLD = 1;

    private int loadMoreThreshold = LOAD_MORE_THRESHOLD;
    private LoadMore loadMoreListener;

    OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (mCachedPageListener != null) {
                mCachedPageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (mCachedPageListener != null) {
                mCachedPageListener.onPageSelected(position);
            }
            if (getAdapter() != null) {
                int count = getAdapter().getCount();
                int currentItem = getCurrentItem();
                if (currentItem >= (count - loadMoreThreshold)) {
                    if (loadMoreListener != null) {
                        loadMoreListener.onLoadMore(LoadMoreViewPager.this);
                    }
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (mCachedPageListener != null) {
                mCachedPageListener.onPageScrollStateChanged(state);
            }
        }
    };
    private OnPageChangeListener mCachedPageListener;

    @Override
    public void setOnPageChangeListener(OnPageChangeListener listener) {
        if (!listener.equals(mOnPageChangeListener)) {
            this.mCachedPageListener = listener;
        } else {
            super.setOnPageChangeListener(listener);
        }
    }

    public void setLoadMoreThreshold(int loadMoreThreshold) {
        this.loadMoreThreshold = loadMoreThreshold;
    }

    public LoadMoreViewPager(Context context) {
        super(context);
        setOnPageChangeListener(mOnPageChangeListener);
    }

    public LoadMoreViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        setOnPageChangeListener(mOnPageChangeListener);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadMoreViewPager);
            loadMoreThreshold = array.getInteger(R.styleable.LoadMoreViewPager_threshold,
                    LOAD_MORE_THRESHOLD);
        }
    }


    public void setLoadMoreListener(LoadMore loadMoreListener) {
        this.loadMoreListener = loadMoreListener;
    }

    public interface LoadMore {
        public void onLoadMore(ViewPager pager);
    }
}

@@ -174,16 +174,42 @@
 buteSet;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -482,25 +482,78 @@
 reListener;%0A
+    private OnPageChangeListener mCachedPageListener;
 %0A
-
     OnPageCh
@@ -1732,50 +1732,316 @@
  %7D;%0A
+%0A
     p
-rivate OnPageChangeListener mCachedPa
+ublic LoadMoreViewPager(Context context) %7B%0A        super(context);%0A        setOnPageChangeListener(mOnPageChangeListener);%0A    %7D%0A%0A    public LoadMoreViewPager(Context context, AttributeSet attrs) %7B%0A        super(context, attrs);%0A        init(context, attrs);%0A        setOnPageChangeListener(mOnPageChan
 geLi
@@ -2046,17 +2046,24 @@
 Listener
+)
 ;
+%0A    %7D
 %0A%0A    @O
@@ -2459,335 +2459,8 @@
  %7D%0A%0A
-    public LoadMoreViewPager(Context context) %7B%0A        super(context);%0A        setOnPageChangeListener(mOnPageChangeListener);%0A    %7D%0A%0A    public LoadMoreViewPager(Context context, AttributeSet attrs) %7B%0A        super(context, attrs);%0A        init(context, attrs);%0A        setOnPageChangeListener(mOnPageChangeListener);%0A    %7D%0A%0A
     
@@ -2853,32 +2853,32 @@
 MoreListener) %7B%0A
-
         this.loa
@@ -2910,32 +2910,793 @@
 istener;%0A    %7D%0A%0A
+    @Override%0A    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) %7B%0A        if (getLayoutParams().height %3E 0) %7B%0A            super.onMeasure(widthMeasureSpec, heightMeasureSpec);%0A        %7D else %7B%0A            int height = 0;%0A            for (int i = 0; i %3C getChildCount(); i++) %7B%0A                View child = getChildAt(i);%0A                child.measure(widthMeasureSpec,%0A                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));%0A                int h = child.getMeasuredHeight();%0A                if (h %3E height) %7B height = h; %7D%0A            %7D%0A            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);%0A            super.onMeasure(widthMeasureSpec, heightMeasureSpec);%0A        %7D%0A    %7D%0A%0A
     public inter

allow warap content in viewpager