【问题标题】:Load more recyclerview with superslim library使用 superslim 库加载更多 recyclerview
【发布时间】:2016-03-23 12:32:04
【问题描述】:

我正在使用带有 loadmore 的 Superslim 库,但出现错误。我创建了一个 EndlessRecyclerOnScrollListener 类来加载更多,但它对我不起作用。

我使用以下方式调用它。

 mViews.mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(new LayoutManager(getActivity())) {
            @Override
            public void onLoadMore(int current_page) {
                if (skipItem == 0)
                    skipItem = 1 * current_page;
                else
                    skipItem = skipItem * current_page;

                new ServiceSync().execute("26", String.valueOf(skipItem));
            }
        });


 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 = 4; // 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) linearLayoutManager;
        }

        @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.<init>(SectionData.java:36)
 at com.tonicartos.superslim.LayoutManager.findFirstVisibleItem(LayoutManager.java:156)
 at com.tonicartos.superslim.LayoutManager.findFirstVisibleItemPosition(LayoutManager.java:187)
 at fourever.textile.notificationadapter.notifications$EndlessRecyclerOnScrollListener.onScrolled(notifications.java:235)
 at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3704)
 at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2947)

【问题讨论】:

    标签: android android-layout android-recyclerview superslim


    【解决方案1】:

    这是我的 EndlessRecyclerOnScrollListener,工作正常:

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    
        if(mLinearLayoutManager == null) return;
    
       int visibleItemCount = recyclerView.getChildCount();
       int totalItemCount = mLinearLayoutManager.getItemCount();
       int firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
    
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached
    
            // Do something
            currentPage++;
    
            onLoadMore(currentPage);
    
            loading = true;
        }
    
        if (mLinearLayoutManager.findLastVisibleItemPosition() == totalItemCount - 1) {
            onEnd( totalItemCount );
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      这是一个 LoadMoreRecyclerView 库

      LoadMoreRecyclerView

          loadMoreRecyclerView.setOnLoadMoreListener(new LoadMoreRecyclerView.OnLoadMoreListener() {
              @Override
              public void onLoadMore() {
                  // load data from background.
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-26
        • 2017-02-11
        • 2016-05-17
        • 1970-01-01
        • 2018-02-08
        • 2015-07-30
        • 1970-01-01
        相关资源
        最近更新 更多