我正在使用 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 滚动的滚动速度并以编程方式再次启动滚动,这样用户就不会注意到任何东西?