0
// Touched
public class ChoiceTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {

        switch (motionEvent.getAction()) {

        case MotionEvent.ACTION_MOVE:

            return true;

        case MotionEvent.ACTION_DOWN:
            Viewed = String.valueOf(view.getId());

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

            // Drag Function When Item Touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;

        case MotionEvent.ACTION_UP:
            return true;

        default:
            return true;
        }
    }
}



private class ChoiceDragListener implements OnDragListener {

    // onDrag Method, imagine we dragging numbers from display TextView
    @Override
    public boolean onDrag(View v, DragEvent event) {

        // Android has onDrag action types. there are some kind of action
        switch (event.getAction()) {

            // if drag stared
            case DragEvent.ACTION_DRAG_STARTED:

            break;

            // if drag entered software
            case DragEvent.ACTION_DRAG_ENTERED:

            break;

            // if drag exited, stopped or something
            case DragEvent.ACTION_DRAG_EXITED:

            break;

            // main case, if drag dropped what we do...
            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.KEEP_SCREEN_ON);

                // view dragged item, where numbers will be dragged
                dropTarget = (TextView) v;

                // view dropped item, that will be dropped in drag TextView
                dropped = (TextView) view;

                // view result place, when make math operation, show results
                resultPlace = (TextView) findViewById(R.id.calcResult);


                // simple debug
                // ****** resultPlace.setText( dropped.getText() + " " + dropTarget.getText() );

                // 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());

                // show results
                resultPlace.setText( dropped.getText() + " " + dropTarget.getText() + " " + dropped.getText() );



            break;

            // if drag ended, we did it already successfully
            case DragEvent.ACTION_DRAG_ENDED:

            break;

            // what do default
            default:

            break;
        }
        return true;
    }
}

私は非常に単純なタッチとドラッグのクラスを持っています。私の唯一の質問は、アイテムをドラッグして特定のレイアウトにドロップすると、アイテムも古い位置に保持されるということです。私が触れているアイテムを検出し、ドロップ後に削除するAndroidの方法はありますか?

小さなアプリケーションがあり、数字をクリックしてダッシュボードにドラッグアンドドロップし、それを繰り返してダッシュボードのすべてのアイテムを X と Y の位置にドラッグします。そのため、古い番号スペースを空にして別のスペースを作成したいのです。

ありがとう...

4

2 に答える 2

0

タッチ リスナーでビューの親を取得できます。アクションダウン部分では、親ビューをグローバル変数に保持し、ビューがドロップされたら removeView(View v) 関数で親から削除します。

于 2013-04-10T08:38:20.280 に答える