此外,我刚刚在视图中添加了swipeRefreshLayout,一旦用户滑动刷新,它将清除recyclerview并再次添加数据
现在的问题是,一旦调用要刷新的刷卡,适配器将无法检测到recyclerview的结束
newsSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
generateNews(1);
}
});
adapterNews.setLoadMore(new ToLoadMore() {
@Override
public void LoadMore() {
if (next_offset != 0) {
newsList.add(null);
adapterNews.notifyItemInserted(newsList.size() - 1);
newsApi.GetNews(Constant.ACCESSTOKEN, next_offset).enqueue(new Callback<NewsData>() {
@Override
public void onResponse(Call<NewsData> call, Response<NewsData> response) {
Log.d("PAGINATION", "PAGINATION");
newsList.remove(newsList.size() - 1);
adapterNews.notifyItemRemoved(newsList.size());
newsList.addAll(response.body().getNews());
next_offset = response.body().getNextOffset();
adapterNews.notifyDataSetChanged();
adapterNews.setLoaded();
}
@Override
public void onFailure(Call<NewsData> call, Throwable t) {
}
});
}
}
});
generateNews(1);
return v;
}
产生新闻的功能如下
private void generateNews(final int offset) {
newsApi.GetNews(Constant.ACCESSTOKEN, offset).enqueue(new Callback<NewsData>() {
@Override
public void onResponse(Call<NewsData> call, Response<NewsData> response) {
newsList.clear();
newsList.addAll(response.body().getNews());
next_offset = response.body().getNextOffset();
if (newsSwipeRefreshLayout.isRefreshing()) {
newsSwipeRefreshLayout.setRefreshing(false);
}
news_progressbar.setVisibility(View.GONE);
news_progressText.setVisibility(View.GONE);
newsRecyclerView.setVisibility(View.VISIBLE);
adapterNews.notifyDataSetChanged();
}
@Override
public void onFailure(Call<NewsData> call, Throwable t) {
snackbar = Snackbar.make(getView(), "No Internet Connection", Snackbar.LENGTH_INDEFINITE).setAction("Close", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
generateNews(1);
}
});
snackbar.show();
if (newsSwipeRefreshLayout.isRefreshing()) {
newsSwipeRefreshLayout.setRefreshing(false);
}
}
});
}