android:EndlessRecyclerOnScrollListener导致循环

时间:2016-10-19 17:03:53

标签: android android-recyclerview infinite-scroll

我使用以下代码实现无限滚动。但问题是onScroll()方法不断调用,加载数据会无限循环。我在这里错过了什么?

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 > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (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);
    }
}

在我的活动中:

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

            }
        };
        mRecyclerView.addOnScrollListener(endlessRecyclerOnScrollListener);

1 个答案:

答案 0 :(得分:0)

这就是我无限滚动的方式,它对我来说非常适合。感谢...

private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;
LinearLayoutManager mLayoutManager;


mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    {
        if(dy > 0) //check for scroll down
        {
            visibleItemCount = mLayoutManager.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

            if (loading) 
            {
                if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                {
                    loading = false;
                    Log.v("...", "We reach Last Item!");

                    // fetch new data here or whatever you want.
                    DoPagination(); 

                }
            }
        }
    }
});