我一直试图寻求其他各种问题寻求帮助,但我似乎无法找到解决方案。
我正在使用的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);
}
}
});
}
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在到达最后一个项目时加载更多项目但是获取的帖子与我已经获得的帖子相同。我知道我得到的东西非常简单。任何帮助将不胜感激。
答案 0 :(得分:0)
这是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中设置pagination
和refresh
的默认变量:
private int currentPage = 0;
private boolean isLoadMoreAvailable = true;
拨打电话:
mController.onVideosRequested(mId, false, currentPage);
onLoadMore
:
if (isLoadMoreAvailable) {
mController.onVideosRequested(mId, false, currentPage + 1);
}
将onLoadMore
和Refresh
放在一起的棘手部分是,当您刷新数据时,您必须重置scrollListeners
总计和之前的计数才能使loadMore
仍然可用。< / p>
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;
}