package com.mikepenz.fastadapter_extensions.scroll;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private int mPreviousTotal = 0;
    private boolean mLoading = true;
    private int mVisibleThreshold = 30;
    private int mFirstVisibleItem, mVisibleItemCount, mTotalItemCount;

    private int mCurrentPage = 1;

    private LinearLayoutManager mLinearLayoutManager;

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

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager, int visibleThreshold) {
        this.mLinearLayoutManager = linearLayoutManager;
        this.mVisibleThreshold = visibleThreshold;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        mVisibleItemCount = recyclerView.getChildCount();
        mTotalItemCount = mLinearLayoutManager.getItemCount();
        mFirstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        if (mLoading) {
            if (mTotalItemCount > mPreviousTotal) {
                mLoading = false;
                mPreviousTotal = mTotalItemCount;
            }
        }
        if (!mLoading && (mTotalItemCount - mVisibleItemCount)
                <= (mFirstVisibleItem + mVisibleThreshold)) {

            mCurrentPage++;

            onLoadMore(mCurrentPage);

            mLoading = true;
        }
    }

    public int getTotalItemCount() {
        return mTotalItemCount;
    }

    public int getFirstVisibleItem() {
        return mFirstVisibleItem;
    }

    public int getVisibleItemCount() {
        return mVisibleItemCount;
    }

    public int getCurrentPage() {
        return mCurrentPage;
    }

    public abstract void onLoadMore(int currentPage);
}
@@ -79,33 +79,31 @@
 .widget.
-LinearLayoutManag
+OrientationHelp
 er;%0Aimpo
@@ -144,16 +144,42 @@
 lerView;
+%0Aimport android.view.View;
 %0A%0Apublic
@@ -499,22 +499,29 @@
 private 
-Linear
+RecyclerView.
 LayoutMa
@@ -528,22 +528,16 @@
 nager mL
-inearL
 ayoutMan
@@ -578,38 +578,45 @@
 nScrollListener(
-Linear
+RecyclerView.
 LayoutManager li
@@ -606,38 +606,32 @@
 .LayoutManager l
-inearL
 ayoutManager) %7B%0A
@@ -637,38 +637,32 @@
 %0A        this.mL
-inearL
 ayoutManager = l
@@ -653,38 +653,32 @@
 ayoutManager = l
-inearL
 ayoutManager;%0A  
@@ -729,115 +729,863 @@
 ner(
-LinearLayoutManager linearLayoutManager, int visibleThreshold) %7B%0A        this.mLinearLayoutManager = linear
+RecyclerView.LayoutManager layoutManager, int visibleThreshold) %7B%0A        this.mLayoutManager = layoutManager;%0A        this.mVisibleThreshold = visibleThreshold;%0A    %7D%0A%0A    private int findFirstVisibleItemPosition(RecyclerView recyclerView) %7B%0A        final View child = findOneVisibleChild(0, mLayoutManager.getChildCount(), false, true);%0A        return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    private View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,%0A                                     boolean acceptPartiallyVisible) %7B%0A        OrientationHelper helper;%0A        if (mLayoutManager.canScrollVertically()) %7B%0A            helper = OrientationHelper.createVerticalHelper(mLayoutManager);%0A        %7D else %7B%0A            helper = OrientationHelper.createHorizontalHelper(m
 Layo
@@ -1585,32 +1585,33 @@
 r(mLayoutManager
+)
 ;%0A        this.m
@@ -1608,49 +1608,956 @@
     
-this.mVisibleThreshold = visibleThreshold
+%7D%0A%0A        final int start = helper.getStartAfterPadding();%0A        final int end = helper.getEndAfterPadding();%0A        final int next = toIndex %3E fromIndex ? 1 : -1;%0A        View partiallyVisible = null;%0A        for (int i = fromIndex; i != toIndex; i += next) %7B%0A            final View child = mLayoutManager.getChildAt(i);%0A            final int childStart = helper.getDecoratedStart(child);%0A            final int childEnd = helper.getDecoratedEnd(child);%0A            if (childStart %3C end && childEnd %3E start) %7B%0A                if (completelyVisible) %7B%0A                    if (childStart %3E= start && childEnd %3C= end) %7B%0A                        return child;%0A                    %7D else if (acceptPartiallyVisible && partiallyVisible == null) %7B%0A                        partiallyVisible = child;%0A                    %7D%0A                %7D else %7B%0A                    return child;%0A                %7D%0A            %7D%0A        %7D%0A        return partiallyVisible
 ;%0A  
@@ -2695,17 +2695,16 @@
 x, dy);%0A
-%0A
         
@@ -2777,30 +2777,24 @@
 emCount = mL
-inearL
 ayoutManager
@@ -2842,29 +2842,8 @@
 m = 
-mLinearLayoutManager.
 find
@@ -2867,16 +2867,28 @@
 osition(
+recyclerView
 );%0A%0A    

improve scroll listener to support all layout manager