Android GridView 拖拽Item及滚屏实现
Android GridView 拖拽Item及滚屏实现
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拖拽。
评论暂时关闭