实现分析
1、GridView的显示由列数、数据源和Item布局决定
2、当GridView所有Item布局不足以超出GridView的高度时,GridView是不可滑动的
3、那么可以根据Item的布局大小,设定一页的数据量显示,通过替换此数据源即可实现左右翻页
核心代码
// 每一页Item个数(根据实际情况修改)
private int pageItemCount = 8;
// 当前页码
private int currentPage = 1;
// 总页码
private int totalPage = 1;
// 总数据源
private List<Kge> kgeList = new ArrayList<>();
// 当前页显示的数据源
private List<Kge> currentList = new ArrayList<>();
// -------- 获取数据源 --------
kgeList.add...
// 设置当前页数据源
if (kgeList.size() > pageItemCount) {
currentList = kgeList.subList(0, pageItemCount);
} else {
currentList = kgeList;
}
mAdapter.resetData(currentList);
// -------- 计算总页码 --------
if (kgeList.size() % pageItemCount == 0) {
totalPage = kgeList.size() / pageItemCount;
} else {
totalPage = kgeList.size() / pageItemCount + 1;
}
// -------- 上下页切换 --------
//上一页
if (currentPage > 1) {
currentList = kgeList.subList((currentPage - 2) * pageItemCount, (currentPage - 1) * pageItemCount);
mAdapter.resetData(currentList);
currentPage--;
}
// 下一页
if (currentPage < totalPage) {
int nextPageSize = pageItemCount;
if (kgeList.size() < (currentPage + 1) * pageItemCount) {
nextPageSize = kgeList.size() - currentPage * pageItemCount;
}
currentList = kgeList.subList(currentPage * pageItemCount, currentPage * pageItemCount + nextPageSize);
mAdapter.resetData(currentList);
currentPage++;
}