Android 實現(xiàn)蛇形布局排序數(shù)據(jù)

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

但是頭疼的就是座位號是按照蛇形排列的like this




于是開始找各種資料,查,改,最后終于搞定,整理一下,怕自己忘記了。

/**

* 按蛇形布局排序數(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;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容