【问题标题】:Android - Endless scrolling with SuperSLiMAndroid - 使用 SuperSLiM 无限滚动
【发布时间】:2015-08-06 14:25:49
【问题描述】:

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

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

如何实现这个功能?

【问题讨论】:

    标签: android android-recyclerview superslim


    【解决方案1】:

    使用超薄的LayoutManager代替LinearLayoutManager。我已经在一个演示项目中实现了它。行代码是,

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

    我希望这会对你有所帮助。 :)

    【讨论】:

    • java.lang.NullPointerException at com.tonicartos.superslim.SectionData.(SectionData.java:36) at com.tonicartos.superslim.LayoutManager.findFirstVisibleItem(LayoutManager.java:156) at com.tonicartos.superslim.LayoutManager.findFirstVisibleItemPosition(LayoutManager.java:187) atfourever.textile.notificationadapter.notifications$EndlessRecyclerOnScrollListener.onScrolled(notifications.java:246) at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView. java:3704) 我在使用这个时遇到了这个错误。
    • 你如何用recyclerview调用这个类
    • @akhilbatlawala,您可以将它与 recyclerview.setOnScrollListener(..) 一起使用,就像使用 onClickListener 一样。
    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 2018-11-02
    • 2015-01-13
    • 2021-12-20
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多