1、先加载全部数据,然后把数据按照20条一页进行加载(不需要UI主动去调分页)
java
class PageLoad extends Thread {
int page = 0;
List<MultiItemEntity> cBillPickSelectUIS;
public PageLoad(List<MultiItemEntity> entitys) {
Log.e("TAG","分页加载===");
cBillPickSelectUIS = entitys;
}
public void run() {
if (cBillPickSelectUIS == null) return;
if (cBillPickSelectUIS.size() < 20) {
requireActivity().runOnUiThread(() -> { // 这里使用handle去更新就不需要用到requireActivity()
mSelAdapter.setNewData(cBillPickSelectUIS);
});
} else {
HashMap<Integer, List<MultiItemEntity>> hashMap = splitArray(cBillPickSelectUIS, 20);
hashMap.get(page);
hashMap.forEach((integer, multiItemEntities) -> {
// if (integer>4) return; //只展示前4条
try {
Thread.sleep(1000);
requireActivity().runOnUiThread(() -> {// 这里使用handle去更新就不需要用到requireActivity()
mSelAdapter.addData(multiItemEntities);
mSelAdapter.notifyDataSetChanged();
});
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
}
public HashMap<Integer, List<MultiItemEntity>> splitArray(List<MultiItemEntity> originalArray, int chunkSize) {
if (originalArray == null || originalArray.size() == 0 || chunkSize <= 0) {
return new HashMap();
}
//这里如何把originalArray数组拆分成多个数组?每个数组50长度,然后返回HashMap
HashMap<Integer, List<MultiItemEntity>> resultMap = new HashMap<>();
int index = 0;
for (int i = 0; i < originalArray.size(); i += chunkSize) {
List<MultiItemEntity> subList = new ArrayList<>(originalArray.subList(i, Math.min(i + chunkSize, originalArray.size())));
resultMap.put(index++, subList);
}
return resultMap;
}
}
调用:
java
new PageLoad(cBillPickSelectUIS).start();
2、异步任务AsyncTask
java
private class LoadDataTask extends AsyncTask<Void, Void, List<MultiItemEntity>> {
private int selflag;
private int patienttypeflag;
private String sDate;
private String eDate;
private List<CPickBillSelectDept> dlist;
private int pickOrCancel;
public LoadDataTask(int selflag, int patienttypeflag, String sDate, String eDate, List<CPickBillSelectDept> dlist, int pickOrCancel) {
this.selflag = selflag;
this.patienttypeflag = patienttypeflag;
this.sDate = sDate;
this.eDate = eDate;
this.dlist = dlist;
this.pickOrCancel = pickOrCancel;
}
@Override
protected List<MultiItemEntity> doInBackground(Void... voids) {
return billPickRepository.getPickBillSelList(selflag, patienttypeflag, sDate, eDate, dlist, pickOrCancel);// 耗时任务
}
@Override
protected void onPostExecute(List<MultiItemEntity> result) {
// 在这里处理后台任务返回的结果,例如更新UI
billpicksellist.postValue(result);
}
}