0

当我使用 SuperSLiM 库时,有什么方法可以实现无限滚动以加载更多项目?

通常我习惯于使用 LinearLayoutManager.findFirstVisibleItemPosition() 方法来帮助我知道何时需要加载更多的项目......但是现在,使用 SuperSLiM 的线性布局我不能使用它。

我该如何实现这个功能?

4

1 回答 1

1

代替 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);
}

我希望这能帮到您。:)

于 2015-09-12T12:46:33.793 回答