onLoadMoreListener赢得';t停止加载-回收视图-Android

onLoadMoreListener赢得';t停止加载-回收视图-Android,android,android-recyclerview,Android,Android Recyclerview,我有一个用户提要,如果当前用户正在跟踪,则将显示用户的帖子,目前我的loadMoreListener工作正常,当它们到达RecyclerView的底部时,将加载下一批图像。但是,当我到达列表中的最后一项(提要仅显示50篇文章)时,它将尝试加载更多内容,并导致OutOfMemoryException。我已经在适配器中限制了列表的大小,但目前我似乎无法确定用户何时触底以停止显示进度并停止OnLoadMoreListener触发。这就是我迄今为止所尝试的: 适配器构造函数: public Recyc

我有一个用户提要,如果当前用户正在跟踪,则将显示用户的帖子,目前我的
loadMoreListener
工作正常,当它们到达
RecyclerView
的底部时,将加载下一批图像。但是,当我到达列表中的最后一项(提要仅显示50篇文章)时,它将尝试加载更多内容,并导致
OutOfMemoryException
。我已经在适配器中限制了列表的大小,但目前我似乎无法确定用户何时触底以停止显示进度并停止OnLoadMoreListener触发。这就是我迄今为止所尝试的:

适配器构造函数:

 public RecyclerViewAdapterAllFeeds(Context context,
                                       final ArrayList<AllFeedsDataModel> previousPostsList, final boolean profile, RecyclerView recyclerView, final ProgressBar progressBar) {
        this.context = context;
        this.previousPostsList = previousPostsList;
        this.profile = profile;
        if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    progressBar.setVisibility(View.VISIBLE);
                        if (getItemCount() > LOADED_POSTS - 2) {
                            totalItemCount = getItemCount();
//                            total_posts = getItemCount();
                            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                            if (lastVisibleItem <= TOTAL_POSTS) {
                                if (!loading && totalItemCount <= (lastVisibleItem)) {
                                    if (onLoadMoreListener != null) {
                                        onLoadMoreListener.onLoadMore();
                                    }
                                    loading = true;
                                }
                            }
                        }
                }
            });
        }
    }

我的问题是,如果帖子总数小于限制,我如何停止加载,或者如何知道最终项目何时可见

首先创建EndlessRecycleWebScrollListener.java:

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;

    RecyclerView.LayoutManager mLayoutManager;

    public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
    }

    public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
    }

    public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
    }

    public int getLastVisibleItem(int[] lastVisibleItemPositions) {
        int maxSize = 0;
        for (int i = 0; i < lastVisibleItemPositions.length; i++) {
            if (i == 0) {
                maxSize = lastVisibleItemPositions[i];
            } else if (lastVisibleItemPositions[i] > maxSize) {
                maxSize = lastVisibleItemPositions[i];
            }
        }
        return maxSize;
    }

    // This happens many times a second during a scroll, so be wary of the code you place here.
    // We are given a few useful parameters to help us work out if we need to load some more data,
    // but first we check if we are waiting for the previous load to finish.
    @Override
    public void onScrolled(RecyclerView view, int dx, int dy) {
        int lastVisibleItemPosition = 0;
        int totalItemCount = mLayoutManager.getItemCount();

        if (mLayoutManager instanceof StaggeredGridLayoutManager) {
            int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
            // get maximum element within the list
            lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
        } else if (mLayoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        } else if (mLayoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        }

        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }
        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        // threshold should reflect how many total columns there are too
        if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
            currentPage++;
            onLoadMore(currentPage, totalItemCount);
            loading = true;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount);

}

查看我用于无限滚动的代码:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                if (BuildConfig.DEBUG)
//                    Log.d(TAG, "onScrolled: [x, y]=[" + dx + ", " + dy + "], count=[" + recyclerView.getLayoutManager().getChildCount() + ", " + recyclerView.getLayoutManager().getItemCount() + ", " + ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition() + "]");
                if (hasMoreRequest) {
                    if (dy > 0) { //check for scroll down
                        checkForMoreLoad();
                    }
                }
            }
        });

private void checkForMoreLoad() {
        final Handler handler = new Handler();
        Runnable checkForMoreData = new Runnable() {
            @Override
            public void run() {
                if (null != recyclerView) {
                    int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                    int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                    int pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                    onScrollToLoad(visibleItemCount, pastVisibleItems, totalItemCount);
                }
            }
        };
        handler.postDelayed(checkForMoreData, 100);
    }

    private void onScrollToLoad(int visibleItemCount, int pastVisibleItems, int totalItemCount) {
        if ((visibleItemCount + pastVisibleItems) + 4 >= totalItemCount && hasMoreRequest) {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "onScroll lastInScreen - so load more");
            startNetworkCall(page + 1);
        }
    }
在从API获取数据之后,如果之后有更多数据,我将再次调用
checkForMoreLoad()


onScrollToLoad()
中包含
(visibleItemCount+pastVisibleItems)+4
的行中包含
4
。这是为了在到达卷轴末端之前启动API。

你能解释一下这对我的情况有什么好处吗?如果我将来想更改它,它是否适合任何数量的帖子?当你到达列表底部时,删除OnScrollListener。是的,这是所有数字的工作岗位。我会测试出来,让你知道巴德。我给它一个去巴德,让你know@BIW当然试着告诉我怎么用的我用了另一个答案伙计,还没测试过
recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(recyclerView.getLayoutManager()) {
            @Override
            public void onLoadMore(int page, int totalItemsCount) {

                //TODO Add logic for load more posts or show message if posts is ended.

            }
        });
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                if (BuildConfig.DEBUG)
//                    Log.d(TAG, "onScrolled: [x, y]=[" + dx + ", " + dy + "], count=[" + recyclerView.getLayoutManager().getChildCount() + ", " + recyclerView.getLayoutManager().getItemCount() + ", " + ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition() + "]");
                if (hasMoreRequest) {
                    if (dy > 0) { //check for scroll down
                        checkForMoreLoad();
                    }
                }
            }
        });

private void checkForMoreLoad() {
        final Handler handler = new Handler();
        Runnable checkForMoreData = new Runnable() {
            @Override
            public void run() {
                if (null != recyclerView) {
                    int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                    int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                    int pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                    onScrollToLoad(visibleItemCount, pastVisibleItems, totalItemCount);
                }
            }
        };
        handler.postDelayed(checkForMoreData, 100);
    }

    private void onScrollToLoad(int visibleItemCount, int pastVisibleItems, int totalItemCount) {
        if ((visibleItemCount + pastVisibleItems) + 4 >= totalItemCount && hasMoreRequest) {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "onScroll lastInScreen - so load more");
            startNetworkCall(page + 1);
        }
    }