繁体   English   中英

使用Parse和RecyclerView进行分页

[英]Pagination using Parse and RecyclerView

我一直试图寻找其他各种问题寻求帮助,但我似乎无法找到解决方案。

我正在使用的OnScrollListener: 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); } } }); } 

通过使用上面的代码,我可以让RecyclerView在到达最后一个项目时加载更多项目但是获取的帖子与我已经获得的帖子相同。 我知道我得到了一些非常简单的错误。 任何帮助将非常感激。

以下是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;
    }

在ui中为paginationrefresh设置默认变量:

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

打电话给你:

mController.onVideosRequested(mId, false, currentPage);

并且onLoadMore

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

onLoadMoreRefresh一起使用的棘手部分是当您刷新数据时,您必须重置scrollListeners总计和之前的计数以保持loadMore仍然可用。

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

最后这里是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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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