android: EndlessRecyclerOnScrollListener causes loop
<p>I use the following code to implement the infinite scroll. but the problem is the <code>onScroll()</code> method keeps calling continuously and loading data falls to an endless loop. what am I missing here?</p>

<pre><code>public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    public int previousTotal = 0; // The total number of items in the dataSet after the last load
    public  boolean loading = true; // True if we are still waiting for the last set of data to load.
    public int visibleThreshold = 0; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;
    public int current_page = 1;
    private LinearLayoutManager mLinearLayoutManager;


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

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

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManager.getItemCount();
        firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount &gt; previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)) {

            current_page++;
            onLoadMore(current_page);
            loading = true;
        }
    }
    public abstract void onLoadMore(int current_page);

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }
}
</code></pre>

<p>and in my activity:</p>

<pre><code>endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(mLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                  loadData()

            }
        };
        mRecyclerView.addOnScrollListener(endlessRecyclerOnScrollListener);
</code></pre>