需求背景:两个接口获取列表数据,(实时流水与历史流水)。实时流水接口返回数据条数不定,不能分页返回。历史流水可分页返回。这两个接口揉成一个列表,要求能正常返回分页数据。
算法说明:
1.获取全部实时流水(currentList)
2. 计算实时流水能填充的页码(cPage=currentList/pageSize)与余数(cRemainder = currentList%pageSize)
3. 判断当前所需页码。
a. pageNo < cPage : 直接截取currentList返回
b. pageNo > cPage则:
调用两页历史流水,前一页(prePage = pageNo - cPage - 1)与当前页(curPage = pageNo - cPage)
如果prePage == 0, 则截取实时流水最后cRemainder条
如果prePage > 0,则获取历史流水,并截取最后cRemainder条
当前页流水截取前(pageSize - cRemainder) 条。
记得做数据判断避免IndexOutOfBoundsException与NullPointerException
第一页 | 第二页 | 第三页 |
---|---|---|
currentData | currentData (余数) | historyData |
currentData | currentData (余数) | historyData |
currentData | historyData | historyData |
currentData | historyData | historyData |
currentData | historyData | historyData |
currentData | historyData | historyData |