3

I'm using an EndlessOnScrollListener (copyright by some genius on the net) to handle an endless Timeline in a RecyclerView. The EndlessOnScrollListener basically looks like this:

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 20; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    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 = LinearLayoutManager.findFirstVisibleItemPosition();

        // recalculate parameters, after new data has been loaded, reset loading to false
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }

        // if visibleThreshold has been reached on the upper (time-wise) side of the Timeline, load next data
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            loadNext();
            loading = true;
        }

        // if visibleThreshold has been reached on the lower side of the Timeline, load previous data
        if (!loading && (firstVisibleItem - visibleThreshold <= 0)) {
            loadPrevious();
            loading = true;
        }
    }

    public abstract void loadNext();

    public abstract void loadPrevious();
}

I added the loadPrevious() part because I want to make the list (the Timeline) endless in both directions.

In the implementation of the loadPrevious(), I add the days of X months to the dataset of my RecyclerView, recalculate my current scroll position and then programmatically scroll to that new position, to give the user the impression of continuous scrolling. The problem is, the moment I do that, scrolling stops and the RecyclerView snaps to the position (obviously). To continue scrolling a new fling is needed.

Question: is there a way to somehow record the scrolling speed the RecyclerView was scrolling at and programmatically fire up the scrolling again, so that the user doesn't notice anything?

kazume
  • 1,063
  • 1
  • 11
  • 22

0 Answers0