如果我的Feed视图中存在差异,我很难显示差距。让我解释一下gap的含义。在我的Feed列表视图中,我一次得到20个结果并将它们保存到本地缓存(GeenDAO DB)并显示在recylerview中。现在的问题是当用户在1天或很长时间后访问应用程序然后在打开供稿视图时他可以看到最新的20个结果,之后看到缓存的旧结果但是在20个项目之后可能存在许多项目。因此,我不知道如何检测缺失项目的开始,并向用户显示按钮,以便在20个项目之后调用和获取结果。
我应该检查缓存结果列表的第一个项目ID和最新列表的最后一个项目ID,并比较两者是否相等,这意味着存在差距并显示"显示更多提要"按钮。这是一个好方法吗?
这是我的代码:
OnViewCreated()方法:我显示缓存中的结果:
List<LatestFeedsItem> latestFeedsItemList = LocalStorageUtil.getDetail(new LatestFeedsItem(), LatestFeedsItemDao.Properties.DateCreated);
mAdapter = new LatestFeedsAdapter(mContext, (latestFeedsItemList != null && !latestFeedsItemList.isEmpty()) ? latestFeedsItemList : new ArrayList<>());
setFeedItemClickListener(mAdapter);
rvFeeds.setAdapter(mAdapter);
然后立即致电服务器以获取最新的20个结果:
// Call to get latest feeds
getLatestFeeds();
我正在使用分页在滚动条上加载下一个20个结果:
rvFeeds.addOnScrollListener(new PaginationScrollListener(mLayoutManager) {
@Override
protected void loadMoreItems() {
isLoading = true;
currentPage += 1;
// mocking network delay for API call
new Handler().postDelayed(() -> getLatestFeeds(), 200);
}
@Override
public boolean isCursorAvailable() {
return curser != null;
}
@Override
public boolean isLastPage() {
return isLastPage;
}
@Override
public boolean isLoading() {
return isLoading;
}
});
现在这是我的getLatestFeeds()方法:
private void getLatestFeeds() {
if (currentPage <= 1) {
mAppController.getLatestFeeds(null, getLatestFeedsCallBack());
} else {
if (curser != null) {
mAdapter.addLoadingFooter();
mAppController.getLatestFeeds(curser, getLatestFeedsCallBack());
}
}
}
这是getLatestFeedsCallBack()方法:
private ICallback<LatestFeedsResponse.LatestFeedsResult> getLatestFeedsCallBack() {
return new ICallback<LatestFeedsResponse.LatestFeedsResult>() {
@Override
public void getResponse(LatestFeedsResponse.LatestFeedsResult latestFeedsResult) {
mSwipeRefreshLayout.setRefreshing(false);
mProgressBar.setVisibility(View.GONE);
mAdapter.removeLoadingFooter();
if(latestFeedsResult!=null&&latestFeedsResult.getMatches()!=null) {
List<LatestFeedsItem> latestFeedsItems = latestFeedsResult.getMatches();
if (latestFeedsItems != null && !latestFeedsItems.isEmpty()) {
// Store response in cache
for (LatestFeedsItem latestFeedsItem : latestFeedsItems) {
setCreatorUploader(latestFeedsItem);
}
List<LatestFeedsItem> list = LocalStorageUtil.getDetail(new LatestFeedsItem(), LatestFeedsItemDao.Properties.DateCreated);
if(list!=null&&!list.isEmpty()){
LatestFeedsItem item = list.get(0);
String dbNewestItemId = item.getId();
LatestFeedsItem item1 = latestFeedsItems.get(latestFeedsItems.size());
String newestOldestItemId = item1.getId();
if(!dbNewestItemId.equalsIgnoreCase(newestOldestItemId)){
//Add a null item to show button and onclick again call getLatestFeed() method with curser.
}
}
LocalStorageUtil.put(latestFeedsItems.toArray());
mAdapter.addAll(latestFeedsItems);
if (latestFeedsResult.getCurs() != null) {
Log.d(sTAG, "Curser :" + latestFeedsResult.getCurs());
curser = latestFeedsResult.getCurs();
} else {
curser = null;
isLastPage = true;
//end of page
}
Log.d(sTAG, "Number of data received: " + latestFeedsItems.size());
} else {
curser = null;
isLastPage = true;
mProgressBar.setVisibility(View.GONE);
}
}else {
curser = null;
isLastPage = true;
mProgressBar.setVisibility(View.GONE);
}
isLoading = false;
}
@Override
public void onFailure(VolleyError volleyError) {
volleyError.printStackTrace();
mAdapter.removeLoadingFooter();
mSwipeRefreshLayout.setRefreshing(false);
mProgressBar.setVisibility(View.GONE);
isLoading = false;
String error = ParseNetworkErrors.parseVolleyError(volleyError);
if (!error.isEmpty()) {
if (error.equalsIgnoreCase(PlownsApplication.getInstance().mFirebaseRemoteConfig.getString("volley_entity_not_found_error"))) {
error = String.format(PlownsApplication.getInstance().mFirebaseRemoteConfig.getString("volley_entity_not_found_error"), "feed");
}
Toast.makeText(getActivity(), error != null ? error : "Oops! Some thing went wrong please try again later.", Toast.LENGTH_SHORT).show();
}
}
};
}
我知道这个问题太长了,但有必要解释一切。 请给我一些建议。感谢