加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_郴州站长网 (http://www.0735zz.com/)- 云通信、区块链、物联设备、云计算、站长网!
当前位置: 首页 > 教程 > 正文

Android GridView 拖拽Item及滚屏达成

发布时间:2021-12-16 13:49:29 所属栏目:教程 来源:互联网
导读:Android GridView 拖拽Item及滚屏实现,针对以前修改的,进行了再修改。 这次就能很好的实现了长按,然后向下拖动,背景的Item也向上的效果。 注要对如下函数进行了修改: private void onDrag(int x, int y) { if (dragImageView != null) { windowParams.al
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拖拽。

(编辑:开发网_郴州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读