创建启动器,就像在android中拖放多个屏幕一样

时间:2015-03-20 07:10:37

标签: android android-animation android-scroll

我想实现一个布局,我可以拖动图像并将其拖放到另一个类似于android启动器的屏幕,我们可以将应用程序图标放在一组水平滚动的屏幕上。我不知道如何开始和从哪里开始。我正在考虑实现一个比屏幕大的布局,然后在用户触摸图像时立即开始自动滚动。丢弃图像的位置将被修复。任何参考或更好的方法来实现这个?

2 个答案:

答案 0 :(得分:0)

  1. 将ViewPager用于多个页面。
  2. 在每个viewpager页面上的ViewPager上使用GridView(在片段中)。
  3. viewPager上的setOnDragListener只有一次// important
  4. 在页面//重要
  5. 上的每个GridView上设置setOnDragListener
  6. 在ACTION_DROP上的目标GridView上获取position = pointToPosition(event.getX(),event.getY())(在这里添加你的图标或者你移动的东西)。
  7. ACTION_DRAG_END用于从源GridView中删除。
  8. 使用gridView中的ACTION_DRAG_EXIT将viewPager页面更改为&来回。
  9. 注意:我假设您正在使用gridView.setOnItemLongClickListener()用于startDrag()等。请在Android网站上提及。

答案 1 :(得分:0)

我刚刚写了这个:(它暂时工作)

  1. 只在ViewPager上添加addLragListener。您不需要在gridviews上添加DragListener。

  2. 获取Drop在目标网格上的位置。

  3. 跟踪源GridView和Target Gridview并使用它来获取其适配器。

  4. 然后在适配器之间操作Drop。

  5. public void setDragListener() {
        pager = getViewPager();
    
        dragListener = new View.OnDragListener() {
            GridViewAdapter sourceAdapter = null;
            GridViewAdapter targetAdapter = null;
            int currentOffset = 0;
    
            @Override
            public boolean onDrag(View v, DragEvent event) {
                int currentX = -1;
                int currentY = -1;
    
                switch (event.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        if (!pager.isFakeDragging()) {
                            pager.beginFakeDrag();
                        }
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        pager.setCurrentItem(pager.getCurrentItem());
                        currentOffset = (int) (event.getX());
                        if (!pager.isFakeDragging()) {
                            pager.beginFakeDrag();
                        }
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DRAG_LOCATION:
                        if (pager != null) {
                            if (!pager.isFakeDragging()) {
                                pager.beginFakeDrag();
                            }
                            if (pager.isFakeDragging()) {
                                int maxWidth = pager.getWidth() * pager.getCount();
                                int offset = (int) (event.getX());
                                int scrollX = getScrollX();
                                int dragBy = (int) -1 * (offset - currentOffset);
                                currentOffset = offset;
                                pager.fakeDragBy(dragBy * pager.getCount());
                                int  scrolledPage = (int) (offset + scrollX) / pager.getWidth();
                                if (pager.getCurrentItem() != scrolledPage) {
                                    pager.setCurrentItem(Math.abs(scrolledPage));
                                }
                            }
                        }
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                        }
                        break;
                    case DragEvent.ACTION_DROP:
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                        }
                        currentX = (int) event.getX();
                        currentY = (int) event.getY();
    
                        // u can use meta data and other info which u passed when longPress
                        // u can set sourcePage and sourcePosition in meta data.
    
                        metaMove = event.getLocalState(); 
    
                        sourceAdapter = // get Source Gridviews adapter
                        targetAdapter = // get Target GridViews Adapter ie current Page's gridviews adapter.
                        GridView g = // get The gridview current Page gridview
                        if (g != null) {
                            int position = g.pointToPosition(currentX + (getScrollX() - (pager.getWidth() * pager.getCurrentItem())), currentY);
                            metaMove.targetPosition = position;
                            metaMove.targetPage = currentPage;
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    // u can move from source to target by inset and remove.
                                    // use some broadcast function to reset if page does not have any item left while moving. Ur logic here.
                                    move(sourceAdapter, targetAdapter, metaMove);
                                }
                            });
                        }
                        break;
                    default:
                        break;
                }
                return true;
            }
        };
        pager.setOnDragListener(dragListener);
    }
    
    public int getScrollX() { 
          int maxWidth = pager.getWidth() * pager.getCount();
          int scrollX = pager.getScrollX();
                          if (pager.getScrollX() < 0) {
                                scrollX = maxWidth + scrollX - pager.getWidth();
                            } else if (pager.getScrollX() == 0) {
                                if (pager.getCurrentItem() == (pager.getCount() - 1)) {
                                    scrollX = maxWidth + scrollX - pager.getWidth();
                                }
                            }
       return scrollX;
    }
    

    Use following onLongPress on your sourceGrid:

                    metaMove.sourcePage = // sourcePage;
                    metaMove.sourcePosition = position;
                    metaMove.targetPosition = -1; // to be set on drop
                    metaMove.targetPage = -1 // to be set on drop;
    
                    boolean dragStarted = view.startDrag(null, 
                            myShadow,  
                            metaMove,  
                            0          
                    );
    

    public void setDragListener() { pager = getViewPager(); dragListener = new View.OnDragListener() { GridViewAdapter sourceAdapter = null; GridViewAdapter targetAdapter = null; int currentOffset = 0; @Override public boolean onDrag(View v, DragEvent event) { int currentX = -1; int currentY = -1; switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: if (!pager.isFakeDragging()) { pager.beginFakeDrag(); } break; case DragEvent.ACTION_DRAG_ENTERED: pager.setCurrentItem(pager.getCurrentItem()); currentOffset = (int) (event.getX()); if (!pager.isFakeDragging()) { pager.beginFakeDrag(); } break; case DragEvent.ACTION_DRAG_EXITED: break; case DragEvent.ACTION_DRAG_LOCATION: if (pager != null) { if (!pager.isFakeDragging()) { pager.beginFakeDrag(); } if (pager.isFakeDragging()) { int maxWidth = pager.getWidth() * pager.getCount(); int offset = (int) (event.getX()); int scrollX = getScrollX(); int dragBy = (int) -1 * (offset - currentOffset); currentOffset = offset; pager.fakeDragBy(dragBy * pager.getCount()); int scrolledPage = (int) (offset + scrollX) / pager.getWidth(); if (pager.getCurrentItem() != scrolledPage) { pager.setCurrentItem(Math.abs(scrolledPage)); } } } break; case DragEvent.ACTION_DRAG_ENDED: if (pager.isFakeDragging()) { pager.endFakeDrag(); } break; case DragEvent.ACTION_DROP: if (pager.isFakeDragging()) { pager.endFakeDrag(); } currentX = (int) event.getX(); currentY = (int) event.getY(); // u can use meta data and other info which u passed when longPress // u can set sourcePage and sourcePosition in meta data. metaMove = event.getLocalState(); sourceAdapter = // get Source Gridviews adapter targetAdapter = // get Target GridViews Adapter ie current Page's gridviews adapter. GridView g = // get The gridview current Page gridview if (g != null) { int position = g.pointToPosition(currentX + (getScrollX() - (pager.getWidth() * pager.getCurrentItem())), currentY); metaMove.targetPosition = position; metaMove.targetPage = currentPage; getActivity().runOnUiThread(new Runnable() { @Override public void run() { // u can move from source to target by inset and remove. // use some broadcast function to reset if page does not have any item left while moving. Ur logic here. move(sourceAdapter, targetAdapter, metaMove); } }); } break; default: break; } return true; } }; pager.setOnDragListener(dragListener); } public int getScrollX() { int maxWidth = pager.getWidth() * pager.getCount(); int scrollX = pager.getScrollX(); if (pager.getScrollX() < 0) { scrollX = maxWidth + scrollX - pager.getWidth(); } else if (pager.getScrollX() == 0) { if (pager.getCurrentItem() == (pager.getCount() - 1)) { scrollX = maxWidth + scrollX - pager.getWidth(); } } return scrollX; }