Android - 使用 SuperSLiM 无限滚动
Android - Endless scrolling with SuperSLiM
当我使用 SuperSLiM 库时,有什么方法可以实现无限滚动以加载更多项目?
通常我习惯使用 LinearLayoutManager.findFirstVisibleItemPosition() 方法来帮助我知道什么时候需要加载更多的元素...但是现在,使用 SuperSLiM 的线性布局我无法使用它。
我该如何实现这个功能?
代替LinearLayoutManager,使用superslim的LayoutManager。我已经在其中一个演示项目中实现了它。行代码是,
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 = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LayoutManager mlayoutManager;
public EndlessRecyclerOnScrollListener(LayoutManager linearLayoutManager) {
this.mlayoutManager = layoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mlayoutManager.getItemCount();
firstVisibleItem = mlayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something here
current_page++;
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
希望对您有所帮助。 :)
当我使用 SuperSLiM 库时,有什么方法可以实现无限滚动以加载更多项目?
通常我习惯使用 LinearLayoutManager.findFirstVisibleItemPosition() 方法来帮助我知道什么时候需要加载更多的元素...但是现在,使用 SuperSLiM 的线性布局我无法使用它。
我该如何实现这个功能?
代替LinearLayoutManager,使用superslim的LayoutManager。我已经在其中一个演示项目中实现了它。行代码是,
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 = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LayoutManager mlayoutManager;
public EndlessRecyclerOnScrollListener(LayoutManager linearLayoutManager) {
this.mlayoutManager = layoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mlayoutManager.getItemCount();
firstVisibleItem = mlayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something here
current_page++;
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
希望对您有所帮助。 :)