Android GridView 拖拽Item及滾屏實現,針對以前修改的,進行了再修改。
這次就能很好的實現了長按,然後向下拖動,背景的Item也向上的效果。
注要對如下函數進行了修改:
- private void onDrag(int x, int y)
- {
- if (dragImageView != null)
- {
- windowParams.alpha = 0.6f;
- windowParams.x = x - dragPointX + dragOffsetX;
- windowParams.y = y - dragPointY + dragOffsetY;
- // L.l("=================windowParams.y=====000========"+windowParams.y);
- windowManager.updateViewLayout(dragImageView, windowParams);
- }
-
- int tempScrollX = x - dragPointX + dragOffsetX;
- int tempScrollY = y - dragPointY + dragOffsetY;
-
- int rangeY = itemHeight;
- int maxHeight = getHeight() - rangeY;
-
- int position = pointToPosition(x, y);
-
- int gridViewCount = this.getCount();
- int allContainCount = gridViewCount;
- int leftCount = gridViewCount % numColumns;
- if (leftCount != 0)
- {
- allContainCount += (numColumns - leftCount);
- }
- int upMaxPosition = allContainCount - numColumns;
- L.l("==========position:" + position + " max:" + upMaxPosition
- + " count:" + this.getChildCount() + " rangy:" + rangeY);
-
- // 如果position大於最大的item
- if (position >= upMaxPosition || position < numColumns)
- {
-
- L.l("=====last line=======postion:" + position);
- setEnabled(false);
- }
- else
- {
- L.l("=====good========tempScrollY: " + tempScrollY + " rangeY:"
- + rangeY + " maxHeight:" + maxHeight);
-
- if (tempScrollY < rangeY)// 假如漂浮的view已經進入第一行,則把當前的gridView滑出一個
- {
- L.l("=====gridView scroll down=======:" + tempScrollY);
- setEnabled(true);
- int position2 = getFirstVisiblePosition();
- smoothScrollToPosition(position2 - 1);
- // scrollTo(0, -itemHeight);
- }
- else
- if (tempScrollY > maxHeight)
- {
- L.l("=====gridView scroll up=======:" + tempScrollY);
- setEnabled(true);
- int position1 = getLastVisiblePosition();
- smoothScrollToPosition(position1 + 1);
- // scrollTo(0, itemHeight);
- }
- }
-
- }
自此完全搞定Item拖拽。