有業(yè)務(wù)需求,實現(xiàn)座位圖的排列(如下圖要求)


于是開始找各種資料,查,改,最后終于搞定,整理一下,怕自己忘記了。
/**
* 按蛇形布局排序數(shù)據(jù)
*
* @param list
* @param columnCount
* @return
*/
private ListsortData(List list, int columnCount) {
//總行數(shù)(奇數(shù)個元素時,值為為總行數(shù)-1)
? ? int sumLine = list.size() / columnCount;
? ? if (list.size() % columnCount !=0) {
sumLine++;
? ? }
//當(dāng)size不能整除column時,最后一行的剩余元素個數(shù);如果能整除,則不走
? ? int lastLineNodeCount = list.size() % columnCount;
? ? //當(dāng)前行號
? ? 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.奇數(shù)行
*? ? ? ? ? 2.小于columnCount / 2 前后翻轉(zhuǎn)
*? ? ? ? ? 3.當(dāng)前行不是最后一行
* */
? ? ? ? ? ? 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.奇數(shù)行
*? ? ? ? ? 2.小于lastLineNodeCount / 2 前后翻轉(zhuǎn)
*? ? ? ? ? 3.當(dāng)前行是最后一行。
* */
? ? ? ? ? ? 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;
}