首页 > 代码库 > 行排序的逻辑

行排序的逻辑

1.lua脚本部分

--------------------------此部分封装了排序的方法-------------------------------------

         function unlock(sender)
             local senderName=sender:getPropertyByName("name");
             move[senderName]=0;
         end;

         function lock(sender)
            local senderName=sender:getPropertyByName("name");
             move[senderName]=1;
         end;

         function initSort(rowNames)
            sort,revSort,move={},{},{};
            for rowNum,rowName in pairs(rowNames) do
                   sort[rowNum]=rowName;
                   revSort[rowName]=rowNum;
            end;
          end;

         function moveRow(rowName,moveY)
                  local onclick=revSort[rowName];
                  if moveY<0 then
                      near=onclick-1;                                   
                  elseif moveY>0 then
                      near=onclick+1;
                  end
                  local rowNear=sort[near];  
                  local senderOnclick=document:getElementsByName(rowName);
                  local senderNear=document:getElementsByName(rowNear);
                  transition:setStopListener(senderOnclick,unlock);
                  transition:setStopListener(senderNear,unlock);
                  transition:setStartListener(senderOnclick,lock);
                  transition:setStartListener(senderNear,lock);
                  transition:translateY(senderOnclick,moveY,1);                 
                  transition:translateY(senderNear,-moveY,1);
                  sort[onclick],sort[near]=sort[near],sort[onclick]; 

                  revSort[rowName],revSort[rowNear]=revSort[rowNear],revSort[rowName];
         end;


------------------------初始化排序--------------------------------------
         --参数:需要排序的控件名称,在table中的位置与初始位置需相同
         initSort({"div1","div2","div3","div4"});

------------------------针对该页面调用排序-----------------------------------------       
          
         local y=0;

         function clickFun(argument,sender)
            y=y+argument["y"];
            alert(y);
            local rowName=sender:getPropertyByName("name");
            local position=revSort[rowName];              
            local moving=0;
            for i,v in pairs(move) do
                moving=moving+move[v]                                
            end;                    
          --以下是向下移动的代码
               if y>=30 and position~=4 and moving==0 then   
                  moveRow(rowName,60);
                  y=0;  
                end;          
          --以下是向上移动的代码
                if y<=-30 and  position~=1 and moving==0 then
                  moveRow(rowName,-60);
                  y=0;                  
                 end;           
           end;


---------------------------设置监听-------------------------------
            local div2 = document:getElementsByTagName("div2");
            gesture:setDragListener(div2, clickFun);

 

2.页面部分(样式略)

    <body name="body">
    <div class="div_test1" name ="div1" border="0">
         <label>row1</label>
    </div>
    <div class="div_test2" name ="div2" border="0">
         <label>row2</label>
    </div>
    <div class="div_test3" name ="div3" border="0">
         <label>row3</label>
    </div>
    <div class="div_test4" name ="div4" border="0">
          <label>row4</label>
    </div>
    </body>