EndlessScrollListener取消第一页

时间:2018-06-03 11:29:28

标签: android

当我在RecyclerView上使用EndlessScrollListener时 它工作但在第2页加载更多数据时 以前的数据消失(第1页)

    recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.addOnScrollListener(new EndlessScrollListener(linearLayoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
            userpage++;
            GetAds(userpage);
        }


    });

和响应改造

        APIRequests APIRequests = ApiClient.getClient().create(APIRequests.class);
    Call<JSONResponseGetAds> call = APIRequests.GetAds(userpage);
    call.enqueue(new Callback<JSONResponseGetAds>() {
        @Override
        public void onResponse(Call<JSONResponseGetAds> call, Response<JSONResponseGetAds> response) {
            skeletonScreen.hide();
            data = new ArrayList<>(Arrays.asList(response.body().getData()));
            adapter = new DataAdapterAds(getApplicationContext(),data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponseGetAds> call, Throwable t) {
            skeletonScreen.hide();
            Log.d("Error",t.getMessage());
        }
    });

1 个答案:

答案 0 :(得分:0)

将适配器初始化为全局变量,并以这种方式将ArrayList作为新数据传递:

adapter = new DataAdapterAds(getApplicationContext(),data);
recyclerView.setAdapter(adapter);

APIRequests APIRequests = ApiClient.getClient().create(APIRequests.class);
Call<JSONResponseGetAds> call = APIRequests.GetAds(userpage);
call.enqueue(new Callback<JSONResponseGetAds>() {
    @Override
    public void onResponse(Call<JSONResponseGetAds> call, Response<JSONResponseGetAds> response) {
        skeletonScreen.hide();
        data = new ArrayList<>(Arrays.asList(response.body().getData()));
        adapter.addData(data);
    }

    @Override
    public void onFailure(Call<JSONResponseGetAds> call, Throwable t) {
        skeletonScreen.hide();
        Log.d("Error",t.getMessage());
    }
});

在扩展的Adapter类中使用以下逻辑:

public void addData(ArrayList<Data> data) {
    if (this.data == null) {
        this.data = new ArrayList<>();
    }

    if (data != null && !data.isEmpty()) {
        for (int i = 0; i < orders.length(); i++) {
            this.data.add(data);
        }
    }

    notifyDataSetChanged();
}