简体   繁体   中英

Pagination using Parse and RecyclerView

I have been trying to look at various other questions for help however I cannot seem to be able to find a solution.

OnScrollListener that I am using: https://gist.github.com/ssinss/e06f12ef66c51252563e

rvHome.addOnScrollListener(new MainRecyclerViewScrollListener(linearLayoutManager) { @Override public void onLoadMore(int page) { fetchPosts(page); } });

 public void fetchOfflinePosts() { offlineListPosts = db.getAllPosts(); for (Post post : offlineListPosts) { listPosts.add(post); } postAdapter.notifyDataSetChanged(); } public void loadPosts() { if (ConnectivityHelper.isConnected(getContext())) { db.deleteAllPosts(); listPosts.clear(); fetchPosts(0); } else { swipeRefreshLayout.setRefreshing(false); } } public void fetchPosts(int skip) { ParseQuery<ParseObject> findPostsRequest = new ParseQuery<>("Post"); findPostsRequest.setSkip(skip); findPostsRequest.orderByDescending("createdAt"); findPostsRequest.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> posts, ParseException e) { if (e == null) { for (ParseObject _post : posts) { final Post post = new Post(); String objectId = _post.getObjectId(); String username; String imageUrl; if (_post.getString("username") != null) { username = _post.getString("username"); } else { username = ""; } ParseQuery<ParseUser> findProfilePicQuery = ParseUser.getQuery(); findProfilePicQuery.whereEqualTo("username", username); findProfilePicQuery.findInBackground(new FindCallback<ParseUser>() { @Override public void done(List<ParseUser> users, ParseException e) { if (e == null) { for (ParseUser user : users) { if (user.getParseFile("profilePic") != null) { post.setProfilePicUrl(user.getParseFile("profilePic").getUrl()); } else { post.setProfilePicUrl(""); } } } } }); if (_post.getParseFile("photo") != null) { imageUrl = _post.getParseFile("photo").getUrl(); } else { imageUrl = ""; } post.setObjectId(objectId); post.setUsername(username); post.setImageUrl(imageUrl); db.addPost(post); } fetchOfflinePosts(); swipeRefreshLayout.setRefreshing(false); } } }); } 

By using the code above I can get the RecyclerView to load more items once the last item is reached but the posts fetched are the same as the ones I have already got. I know I'm getting something really simple wrong. Any help would be much appreciated.

Here's a working example of Pagination and Refresh data with Parse:

    @Override
    public void onVideosRequested(final int categoryId, final boolean isRefresh, final int page) {
        if (!isRefresh && page == 0) {
            mView.showIndicator();
        }
        ParseQuery<ParseVideo> query = ParseVideo.generateGetVideosByCategoryQuery(categoryId, page, CategoryFragment.VIDEOS_PER_PAGE);
        query.findInBackground(new FindCallback<ParseVideo>() {
            @Override
            public void done(List<ParseVideo> objects, ParseException e) {
                if (!isRefresh) {
                    if (page == 0) {
                        mView.hideIndicator();
                    }
                } else {
                    mView.showHideRefreshLayout(false);
                }
                if (e == null) {
                    if (objects != null && objects.size() > 0) {
                        List<ParseVideo> mList = cleanData(objects);
                        List<ParseVideo> mVideos = DashboardActivity.mVideos.get(categoryId);
                        if (page == 0) {
                            mVideos.clear();
                            mVideos.addAll(mList);
                        } else {
                            mVideos.addAll(mList);
                        }
                        if (objects.size() < CategoryFragment.VIDEOS_PER_PAGE) {
                            mView.isLoadMoreAvailable(false);
                        } else {
                            mView.isLoadMoreAvailable(true);
                        }
                        DashboardActivity.mIndexes.put(categoryId, page);
                        mView.onVideosReceived(mList.size(), page);
                    } else {
                        mView.isLoadMoreAvailable(false);
                    }
                } else {
                    mView.onDefaultError(e.getLocalizedMessage());
                }
            }
        });
    }


    @Override
    public List<ParseVideo> cleanData(List<ParseVideo> data) {
        List<ParseVideo> mList = new ArrayList<>();
        for (ParseVideo pv : data) {
            if (pv != null && !TextUtils.isEmpty(pv.getName()) && !TextUtils.isEmpty(pv.getId())) {
                mList.add(pv);
            }
        }
        return mList;
    }

In ui set default variables for pagination and refresh :

private int currentPage = 0;
private boolean isLoadMoreAvailable = true;

And make your call:

mController.onVideosRequested(mId, false, currentPage);

And onLoadMore :

if (isLoadMoreAvailable) {
     mController.onVideosRequested(mId, false, currentPage + 1);
}

The tricky part with onLoadMore and Refresh together is when you refresh your data you have to reset your scrollListeners total and previous counts to keep loadMore still available.

mHidingScrollListener.setPreviousTotal(0);
mHidingScrollListener.setTotalItemCount(0);

Finally here's the parse query :

public static ParseQuery<ParseVideo> generateGetVideosByCategoryQuery(int categoryId, int page, int displayLimit) {
        ParseQuery<ParseVideo> query = ParseQuery.getQuery(ParseVideo.class);
        query.whereEqualTo(ParseConstants.IS_DELETED, false);
        query.addDescendingOrder(ParseConstants.CREATED_At);
        if (categoryId != ParseConstants.CATEGORY_ID_HOME) {
            query.whereEqualTo(CATEGORY_ID, categoryId);
        }
        query.setLimit(displayLimit);
        query.setSkip(page * displayLimit);
        return query;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM