Android GridView 拖拽Item及滚屏实现


Android GridView 拖拽Item及滚屏实现,针对以前修改的,进行了再修改。

这次就能很好的实现了长按,然后向下拖动,背景的Item也向上的效果。

注要对如下函数进行了修改: 

  1. private void onDrag(int x, int y)  
  2.     {  
  3.         if (dragImageView != null)  
  4.         {  
  5.             windowParams.alpha = 0.6f;  
  6.             windowParams.x = x - dragPointX + dragOffsetX;  
  7.             windowParams.y = y - dragPointY + dragOffsetY;  
  8.             // L.l("=================windowParams.y=====000========"+windowParams.y);   
  9.             windowManager.updateViewLayout(dragImageView, windowParams);  
  10.         }  
  11.   
  12.         int tempScrollX = x - dragPointX + dragOffsetX;  
  13.         int tempScrollY = y - dragPointY + dragOffsetY;  
  14.   
  15.         int rangeY = itemHeight;  
  16.         int maxHeight = getHeight() - rangeY;  
  17.   
  18.         int position = pointToPosition(x, y);  
  19.   
  20.         int gridViewCount = this.getCount();  
  21.         int allContainCount = gridViewCount;  
  22.         int leftCount = gridViewCount % numColumns;  
  23.         if (leftCount != 0)  
  24.         {  
  25.             allContainCount += (numColumns - leftCount);  
  26.         }  
  27.         int upMaxPosition = allContainCount - numColumns;  
  28.         L.l("==========position:" + position + "  max:" + upMaxPosition  
  29.                 + "  count:" + this.getChildCount() + "  rangy:" + rangeY);  
  30.   
  31.         // 如果position大于最大的item   
  32.         if (position >= upMaxPosition || position < numColumns)  
  33.         {  
  34.               
  35.             L.l("=====last line=======postion:" + position);  
  36.             setEnabled(false);  
  37.         }  
  38.         else  
  39.         {  
  40.             L.l("=====good========tempScrollY:  " + tempScrollY + " rangeY:"  
  41.                     + rangeY + " maxHeight:" + maxHeight);  
  42.   
  43.             if (tempScrollY < rangeY)// 假如漂浮的view已经进入第一行,则把当前的gridView滑出一个   
  44.             {  
  45.                 L.l("=====gridView scroll down=======:" + tempScrollY);  
  46.                 setEnabled(true);  
  47.                 int position2 = getFirstVisiblePosition();  
  48.                 smoothScrollToPosition(position2 - 1);  
  49.                 // scrollTo(0, -itemHeight);   
  50.             }  
  51.             else  
  52.                 if (tempScrollY > maxHeight)  
  53.                 {  
  54.                     L.l("=====gridView scroll up=======:" + tempScrollY);  
  55.                     setEnabled(true);  
  56.                     int position1 = getLastVisiblePosition();  
  57.                     smoothScrollToPosition(position1 + 1);  
  58.                     // scrollTo(0, itemHeight);   
  59.                 }  
  60.         }  
  61.   
  62.     }  

自此完全搞定Item拖拽。

相关内容