如何在同一布局中从一个网格视图拖放项目到另一个网格视图?(涉及IT技术)

4

我希望可以在Gingerbread版本中从一个GridView拖动项目到另一个GridView。我调用了onTouchListener和onDragListener,但这些在API 8中不可用。我需要它能够在API 8中正常工作。有人可以帮忙吗?

1个回答

2
我有一个非常简单的解决方案.. 只需添加以下两个函数。
ChoiceTouchListner()
/**
 * 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是你放置按钮/视图的LinearLayout。 initialLayer是你选择按钮/视图的LinearLayout。
你可以将LinearLayout更改为GridLayout。
不要忘记设置。
initialLinear.setOnDragListener(new ChoiceDragListener(linear,initialLinearSecond));
dropTarget.setOnDragListener(new ChoiceDragListener(initialLinear,initialLinearSecond));

这对于API 8不起作用。请发布整个代码以便从一个GridView拖放项目到另一个GridView。 - user2031501
1
这段代码看起来像是从一些流行的在线教程中复制粘贴过来的,但是它存在一些问题。 - Ankur Gautam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接