有业务需求,实现座位图的排列(如下图要求)
于是开始找各种资料,查,改,最后终于搞定,整理一下,怕自己忘记了。
/**
* 按蛇形布局排序数据
*
* @param list
* @param columnCount
* @return
*/
private ListsortData(List list, int columnCount) {
//总行数(奇数个元素时,值为为总行数-1)
int sumLine = list.size() / columnCount;
if (list.size() % columnCount !=0) {
sumLine++;
}
//当size不能整除column时,最后一行的剩余元素个数;如果能整除,则不走
int lastLineNodeCount = list.size() % columnCount;
//当前行号
int currentLineNumber;
for (int i =0; i < list.size(); i++) {
currentLineNumber = i / columnCount;
UploadShowEntity mEntity = list.get(i);
String temp = list.get(i).getZuowei();
if (lastLineNodeCount ==0) {
if (isOddNumber(currentLineNumber) && i % columnCount < columnCount /2 && currentLineNumber <= sumLine -1) {
list.set(i, list.get(columnCount -1 - (i % columnCount) + columnCount * currentLineNumber));
mEntity.setZuowei(temp);
list.set(columnCount -1 - i % columnCount + columnCount * currentLineNumber, mEntity);
}
}else {
/**
* 判断条件:1.奇数行
* 2.小于columnCount / 2 前后翻转
* 3.当前行不是最后一行
* */
if (isOddNumber(currentLineNumber) && i % columnCount < columnCount /2 && currentLineNumber < sumLine -1) {
list.set(i, list.get(columnCount -1 - (i % columnCount) + columnCount * currentLineNumber));
mEntity.setZuowei(temp);
list.set(columnCount -1 - i % columnCount + columnCount * currentLineNumber, mEntity);
}
/**
* 判断条件:1.奇数行
* 2.小于lastLineNodeCount / 2 前后翻转
* 3.当前行是最后一行。
* */
else if (isOddNumber(currentLineNumber) && ((i % columnCount) % lastLineNodeCount) < (lastLineNodeCount /2) && currentLineNumber == sumLine -1) {
list.set(i, list.get(lastLineNodeCount -1 - ((i % columnCount) % lastLineNodeCount) + columnCount * currentLineNumber));
mEntity.setZuowei(temp);
list.set(lastLineNodeCount -1 - ((i % columnCount) % lastLineNodeCount) + columnCount * currentLineNumber, mEntity);
}
}
}
return list;
}