android GridView 拖拽Item及滾屏實現

來源:互聯網
上載者:User
針對以前修改的,進行了再修改。

這次就能很好的實現了長按,然後向下拖動,背景的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大於最大的itemif (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);}elseif (tempScrollY > maxHeight){L.l("=====gridView scroll up=======:" + tempScrollY);setEnabled(true);int position1 = getLastVisiblePosition();smoothScrollToPosition(position1 + 1);// scrollTo(0, itemHeight);}}}自此完全搞定Item拖拽。下載代碼連結:http://download.csdn.net/detail/synwith/4082316 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.