歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android GridView 拖拽Item及滾屏實現

Android GridView 拖拽Item及滾屏實現

日期:2017/3/1 10:29:37   编辑:Linux編程

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. int tempScrollX = x - dragPointX + dragOffsetX;
  12. int tempScrollY = y - dragPointY + dragOffsetY;
  13. int rangeY = itemHeight;
  14. int maxHeight = getHeight() - rangeY;
  15. int position = pointToPosition(x, y);
  16. int gridViewCount = this.getCount();
  17. int allContainCount = gridViewCount;
  18. int leftCount = gridViewCount % numColumns;
  19. if (leftCount != 0)
  20. {
  21. allContainCount += (numColumns - leftCount);
  22. }
  23. int upMaxPosition = allContainCount - numColumns;
  24. L.l("==========position:" + position + " max:" + upMaxPosition
  25. + " count:" + this.getChildCount() + " rangy:" + rangeY);
  26. // 如果position大於最大的item
  27. if (position >= upMaxPosition || position < numColumns)
  28. {
  29. L.l("=====last line=======postion:" + position);
  30. setEnabled(false);
  31. }
  32. else
  33. {
  34. L.l("=====good========tempScrollY: " + tempScrollY + " rangeY:"
  35. + rangeY + " maxHeight:" + maxHeight);
  36. if (tempScrollY < rangeY)// 假如漂浮的view已經進入第一行,則把當前的gridView滑出一個
  37. {
  38. L.l("=====gridView scroll down=======:" + tempScrollY);
  39. setEnabled(true);
  40. int position2 = getFirstVisiblePosition();
  41. smoothScrollToPosition(position2 - 1);
  42. // scrollTo(0, -itemHeight);
  43. }
  44. else
  45. if (tempScrollY > maxHeight)
  46. {
  47. L.l("=====gridView scroll up=======:" + tempScrollY);
  48. setEnabled(true);
  49. int position1 = getLastVisiblePosition();
  50. smoothScrollToPosition(position1 + 1);
  51. // scrollTo(0, itemHeight);
  52. }
  53. }
  54. }

自此完全搞定Item拖拽。

Copyright © Linux教程網 All Rights Reserved