![](/img/trans.png)
[英]How to append the the response of new API hit in previous dataset of RecyclerView while implementing pagination
[英]How to prevent data clear while implementing pagination with recyclerview + GSON
我需要在實現分頁時防止數據清除。 我使用下面的代碼來反序列化 JSON 響應並在我的適配器中輸入。
Gson gson = new Gson();
List<MyDataModel> mcontentList = Arrays.asList(gson.fromJson(getResponse, MyDataModel.class));
mAdapter = new MyDataAdapter(getApplicationContext(), mcontentList.get(0).getResponse().getCoreContent());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.getAdapter().notifyDataSetChanged();
第一組數據一切正常,當我添加更多滾動/分頁時,下一組數據替換了我的舊數據,建議我處理這個問題。
謝謝。
這是因為您每次都在清除舊數據。
在您的適配器中添加此方法
void addData(List<MyDataModel> newData){
for(MyDataModel data:newData)
listData.add(data);
}
進而
if(mAdapter==null){
mAdapter = new MyDataAdapter(getApplicationContext(), mcontentList.get(0).getResponse().getCoreContent());
mRecyclerView.setAdapter(mAdapter);
}
else{
mAdapter.addData(mcontentList);
}
在您的MyDataAdapter
創建一個方法 public 並在調用此方法之前將適配器設置為空列表
void addData(List<Your Object> mcontentList){
yourList.addAll(mcontentList);
notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.