android - 如何将项目从一个 gridview 拖放到同一布局中的另一个 gridview?

标签 android drag-and-drop android-gridview

我想从 Gingerbread 中将项目从一个 gridview 拖到另一个 gridview。我正在调用 onTouchListener、onDragListener,但这些在 api 8 中不可用。我需要,它应该在 api 8 中工作。有人能帮忙吗

最佳答案

我有一个非常简单的解决方案.. 只需添加以下 2 个函数即可。

选择触摸列表()

/**
 * ChoiceTouchListener will handle touch events on draggable views
 * 
 */
@SuppressLint({ "NewApi", "NewApi" })
private final class ChoiceTouchListener implements OnTouchListener {
    @SuppressLint("NewApi")
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            /*
             * Drag details: we only need default behavior - clip data could
             * be set to pass data as part of drag - shadow can be tailored
             */
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            // start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        } else {
            return false;
        }
    }
}

ChoiceDragListener()

    /**
 * DragListener will handle dragged views being dropped on the drop area -
 * only the drop action will have processing added to it as we are not -
 * amending the default behavior for other parts of the drag process
 * 
 */
private class ChoiceDragListener implements OnDragListener {

    private LinearLayout initiallinear;

    public ChoiceDragListener(LinearLayout initialLinear ) {
        this.initiallinear = initialLinear;

    }

    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            // no action necessary
            break;
        case DragEvent.ACTION_DROP:
            // handle the dragged view being dropped over a drop view
            View view = (View) event.getLocalState();
            // stop displaying the view where it was before it was dragged
            view.setVisibility(View.INVISIBLE);
            // view dragged item is being dropped on
            LinearLayout dropTarget = (LinearLayout) v;
            // view being dragged and dropped
            Button dropped = (Button) view;

            // update the text in the target view to reflect the data being
            // dropped
            int recentTag = (Integer) dropped.getTag();

            if (dropped.getParent() == dropTarget) {
                dropTarget.removeView(dropped);
                dropTarget.invalidate();
            } else {

                    initiallinear.removeView(dropped);
                    initiallinear.invalidate();


            }
            try {
                dropTarget.addView(dropped);
                dropTarget.invalidate();
            } catch (Exception e) {
                System.out.println(e);
            }

            dropped.setVisibility(View.VISIBLE);
            dropTarget.invalidate();
            // if an item has already been dropped here, there will be a tag
            Object tag = dropTarget.getTag();

            /*
             * //if there is already an item here, set it back visible in
             * its original place if(tag!=null) { //the tag is the view id
             * already dropped here int existingID = (Integer)tag; //set the
             * original view visible again
             * findViewById(existingID).setVisibility(View.VISIBLE); } //set
             * the tag in the target view being dropped on - to the ID of
             * the view being dropped dropTarget.setTag(dropped.getId());
             */
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}

您的整个代码将在 DragEvent.ACTION_DROP 中完成。 这里的 dropTarget 是放置按钮/ View 的线性布局。 initialLayer 是您选择按钮/ View 的 LinearLayout。

您可以将 LinearLayout 更改为网格布局。

不要忘记设置

initialLinear.setOnDragListener(new ChoiceDragListener(linear,initialLinearSecond));
dropTarget.setOnDragListener(new ChoiceDragListener(initialLinear,initialLinearSecond));

关于android - 如何将项目从一个 gridview 拖放到同一布局中的另一个 gridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15898968/

相关文章:

android - newSpellCheckerSession 总是返回 null

windows - 如何创建拖放式 Strawberry Perl 程序?

animation - 变换 : rotate. 上的 CSS3 动画获取旋转元素当前度数的方法?

java - 如何以编程方式为 Imageview 设置双倍高度?

android - 旧的Android加载到新的eclipse中

drag-and-drop - 未引发 DragDrop 事件

Android:2个不同的 View 意味着2个不同的 Activity ?

android - Monodorid 上的 GridView 适配器

java - RecyclerView 中的网格项未正确显示

android - 如何检测设备旋转以设置 EXIF 方向标签 (Android)