RecyclerView Endless Scrolling Up:如何保持滚动?

时间:2015-12-12 19:27:51

标签: android endlessscroll

我正在使用EndlessOnScrollListener(网上一些天才的版权)来处理RecyclerView中无休止的时间轴。 EndlessOnScrollListener基本上如下所示:

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();
}

我添加了loadPrevious()部分,因为我想在两个方向上使列表(时间轴)无穷无尽。

在loadPrevious()的实现中,我将X个月的日期添加到RecyclerView的数据集中,重新计算当前滚动位置,然后以编程方式滚动到该新位置,以给用户留下连续滚动的印象。问题是,当我这样做时,滚动停止并且RecyclerView捕捉到该位置(显然)。要继续滚动,需要新的投掷。

问题:有没有办法以某种方式记录RecyclerView滚动的滚动速度并以编程方式再次启动滚动,以便用户不会注意到任何内容?

0 个答案:

没有答案