我有这样的方法来初始化一个recyclerview:
private void getSuratList() {
SuratService suratService = ServiceGeneratorUtils.createService(SuratService.class);
Call<SuratList> suratListCall = suratService.getListSurat(user.getId());
suratListCall.enqueue(new Callback<SuratList>() {
@Override
public void onResponse(@NonNull Call<SuratList> call, @NonNull Response<SuratList> response) {
mSuratList = new ArrayList<>();
mSuratList.addAll(Objects.requireNonNull(response.body()).getData());
mMainAdapter.edit().replaceAll(mSuratList).commit();
}
@Override
public void onFailure(@NonNull Call<SuratList> call, @NonNull Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
}
,并且每当到达项目末尾时,它都应该加载更多的项目,并且此代码用于从api加载更多数据:
mActivityHomeBinding.activityMainRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListenerUtils() {
@Override
public void onLoadMore(RecyclerView recyclerView, int currentPage) {
SuratService suratService = ServiceGeneratorUtils.createService(SuratService.class);
Call<SuratList> suratListNextCall = suratService.getListSuratNextPage(user.getId(), Integer.valueOf(currentPage).longValue());
suratListNextCall.enqueue(new Callback<SuratList>() {
@Override
public void onResponse(@NonNull Call<SuratList> call, @NonNull Response<SuratList> response) {
mSuratList.addAll(Objects.requireNonNull(response.body()).getData());
recyclerView.getAdapter().notifyDataSetChanged();
}
@Override
public void onFailure(@NonNull Call<SuratList> call, @NonNull Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
}
});
但是它不会更新适配器