Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android startDrag()之后的代码不执行_Android_Drag And Drop - Fatal编程技术网

Android startDrag()之后的代码不执行

Android startDrag()之后的代码不执行,android,drag-and-drop,Android,Drag And Drop,我要做的是在一个视图上有4个ImageButton,然后当我将一个按钮拖动到另一个按钮时,Logcat中会打印一条消息。问题是,即使拖动事件经过START、ENTERED、LOCATION、exit、DROP、end都没有问题,但是onLongClick的其余部分,v.startDrag之后的代码也不起作用。 提前谢谢 public class HomeAcitivity extends Activity implements OnClickListener, OnLongClickListen

我要做的是在一个视图上有4个ImageButton,然后当我将一个按钮拖动到另一个按钮时,Logcat中会打印一条消息。问题是,即使拖动事件经过START、ENTERED、LOCATION、exit、DROP、end都没有问题,但是onLongClick的其余部分,v.startDrag之后的代码也不起作用。 提前谢谢

public class HomeAcitivity extends Activity implements OnClickListener, OnLongClickListener 
    {
    //int windowWidth = getWindowManager().getDefaultDisplay().getWidth();
    //int windowHeight = getWindowManager().getDefaultDisplay().getHeight();

    public static int action = 0; // what action to take:
                                    // 0: nothing
                                    // 1: song to album
                                    // 2: song to genre
                                    // 3: song to artist

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initialization(savedInstanceState);
    }

    public void initialization(Bundle savedInstanceState)
    {
        ImageButton songsMap = (ImageButton) findViewById(R.id.songsmap);
        songsMap.setContentDescription("none");
        songsMap.setOnClickListener(this);
        songsMap.setOnLongClickListener(this);
        ImageButton albumsMap = (ImageButton) findViewById(R.id.albumsmap);
        albumsMap.setContentDescription("none");
        albumsMap.setOnClickListener(this);
        albumsMap.setOnLongClickListener(this);
        ImageButton artistsMap = (ImageButton) findViewById(R.id.artistsmap);
        artistsMap.setOnClickListener(this);
        artistsMap.setOnLongClickListener(this);
        ImageButton genresMap = (ImageButton) findViewById(R.id.genresmap);
        genresMap.setOnClickListener(this);
        genresMap.setOnLongClickListener(this);
        myDragEventListener mAlbumDragListener = new myDragEventListener();
        albumsMap.setOnDragListener(mAlbumDragListener);
        myDragEventListener mSongDragListener = new myDragEventListener();
        songsMap.setOnDragListener(mSongDragListener);
        myDragEventListener mArtistDragListener = new myDragEventListener();
        artistsMap.setOnDragListener(mArtistDragListener);
        myDragEventListener mGenreDragListener = new myDragEventListener();
        genresMap.setOnDragListener(mGenreDragListener);


    }

    public boolean songtoalbum()
    {
        Log.d("Song to album:", "true");
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

    public void onClick(View arg0) 
    {


    }


    public boolean onLongClick(View v) 
    {
        if (v.getId() == R.id.musicmapparent)
        {
            // here modify wallpaper
            return true;
        }
        // Drag and drop
        ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
        // this part is different from the android dev:
        String[] mimetype = new String[1];
        mimetype[0] = ClipDescription.MIMETYPE_TEXT_PLAIN;

        ClipData dragData = new ClipData((CharSequence) v.getTag(), 
                                        mimetype, 
                                        item);
        View.DragShadowBuilder myShadow;

        switch(v.getId())
        {
        case R.id.albumsmap:
            myShadow = 
                new MyDragShadowBuilder (v, getResources().getDrawable(R.drawable.ic_launcher));        
                break;
        case R.id.songsmap:
            myShadow = 
                new MyDragShadowBuilder (v, getResources().getDrawable(R.drawable.ic_launcher));

            break;
        case R.id.artistsmap:
            myShadow = 
                new MyDragShadowBuilder (v, getResources().getDrawable(R.drawable.ic_launcher));

            break;
        case R.id.genresmap:
            myShadow = 
                new MyDragShadowBuilder (v, getResources().getDrawable(R.drawable.ic_launcher));
            break;
        default:
            myShadow = null;
        }

        //---------------------------------------------------------------------------

        //v.setOnDragListener(mDragListener);
        v.startDrag(dragData,  // the data to be dragged
                myShadow,  // the drag shadow builder
                null,      // no need to use local data
                0          // flags (not currently used, set to 0)
                );

        // Stuck here >"<
        Log.d("Out of Drag", "At least we get here");
        switch (HomeAcitivity.action)
        {
        case 0:
            break;
        case 1:
            Log.d("ACTION: ", "Song to album");

            //songtoalbum();

            break;
        case 2:
            Log.d("ACTION: ", "Song to genre");
            //songtogenre();
            break;
        case 3:
            Log.d("ACTION: ", "Song to artist");
            //songtoartist();
            break;
        default:
            Log.d("ACTION", "nothing");
            break;

        }
        return true;

    }

}


// To create the shadow for the object
class MyDragShadowBuilder extends View.DragShadowBuilder {

    // The drag shadow image, defined as a drawable thing
    private static Drawable shadow;

        // Defines the constructor for myDragShadowBuilder
        public MyDragShadowBuilder(View v, Drawable drawable) {
            super(v);
            shadow = drawable;
        }

        // Defines a callback that sends the drag shadow dimensions and touch point back to the
        // system.
        @Override
        public void onProvideShadowMetrics (Point size, Point touch)
        {
            // Defines local variables
            int width, height;

            // Sets the width of the shadow to half the width of the original View

            width = getView().getWidth();

            // Sets the height of the shadow to half the height of the original View
            height = getView().getHeight();

            // The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
            // Canvas that the system will provide. As a result, the drag shadow will fill the
            // Canvas.
            shadow.setBounds(0, 0, width, height);

            // Sets the size parameter's width and height values. These get back to the system
            // through the size parameter.
            size.set(width, height);

            // Sets the touch point's position to be in the middle of the drag shadow
            touch.set(width / 2, height / 2);
        }

        // Defines a callback that draws the drag shadow in a Canvas that the system constructs
        // from the dimensions passed in onProvideShadowMetrics().
        @Override
        public void onDrawShadow(Canvas canvas) {

            // Draws the ColorDrawable in the Canvas passed in from the system.
            shadow.draw(canvas);
        }
    }

class myDragEventListener implements OnDragListener {
    // This is the method that the system calls when it dispatches a drag event to the
    // listener.
    public boolean onDrag(View v, DragEvent event) {

        // Defines a variable to store the action type for the incoming event
        final int action = event.getAction();

        // Handles each of the expected events
        CharSequence dragData = null;
        switch(action) {

            case DragEvent.ACTION_DRAG_STARTED:

                // Determines if this View can accept the dragged data
                if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    switch(v.getId())
                    {
                    case R.id.albumsmap:

                        break;
                    case R.id.songsmap:
                        v.setContentDescription("songstart");
                        break;

                    case R.id.artistsmap:

                        break;

                    case R.id.genresmap:

                        break;

                    }
                    // As an example of what your application might do,
                    // applies a blue color tint to the View to indicate that it can accept
                    // data.
                    ((ImageView) v).setColorFilter(Color.BLUE);

                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();
                    dragData = (CharSequence) v.getTag();
                    // returns true to indicate that the View can accept the dragged data.
                    return(true);

                    } else {

                    // Returns false. During the current drag and drop operation, this View will
                    // not receive events again until ACTION_DRAG_ENDED is sent.
                    return(false);

                    }
                //break;

            case DragEvent.ACTION_DRAG_ENTERED:

                // Applies a green tint to the View. Return true; the return value is ignored.

                ((ImageView) v).setColorFilter(Color.GREEN);

                // Invalidate the view to force a redraw in the new tint
                v.invalidate();

                return(true);

                //break;

            case DragEvent.ACTION_DRAG_LOCATION:

                //
                switch(v.getId())
                {
                case R.id.albumsmap:
                    ((ImageView) v).setColorFilter(Color.RED);
                    break;
                case R.id.songsmap:
                    ((ImageView) v).setColorFilter(Color.YELLOW);
                    break;

                case R.id.artistsmap:
                    ((ImageView) v).setColorFilter(Color.WHITE);
                    break;

                case R.id.genresmap:
                    ((ImageView) v).setColorFilter(Color.BLACK);
                    break;

                }
                return(true);

                //break;

                case DragEvent.ACTION_DRAG_EXITED:

                    // Re-sets the color tint to blue. Returns true; the return value is ignored.
                    ((ImageView) v).setColorFilter(Color.BLUE);

                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();

                    return(true);

                //break;

                case DragEvent.ACTION_DROP:

                    // Gets the item containing the dragged data
                    ClipData.Item item = event.getClipData().getItemAt(0);

                    // Gets the text data from the item.
                    dragData = item.getText();
                    Log.d("DRAGDATA = ", (String) dragData);
                    Log.d("V.ID = ", Integer.toString(v.getId()));

                    Log.d("Album.ID = ", Integer.toString(R.id.albumsmap));
                    // Displays a message containing the dragged data.
                    //Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_LONG);

                    // Turns off any color tints
                    ((ImageView) v).clearColorFilter();

                    // Invalidates the view to force a redraw
                    v.invalidate();
                    String dragDataString = (String) dragData;
                    Log.d(dragDataString, dragDataString);
                    //
                    switch(v.getId())
                    {
                    case R.id.albumsmap:
                        ((ImageView) v).setColorFilter(Color.RED);

                        if (dragDataString.equals("SONG_MAP_BUTTON"))
                        {
                            HomeAcitivity.action = 1;
                            v.invalidate();

                        }
                        break;
                    case R.id.songsmap:
                        ((ImageView) v).setColorFilter(Color.YELLOW);
                        break;

                    case R.id.artistsmap:
                        if (dragDataString.equals("SONG_MAP_BUTTON"))
                        {
                            HomeAcitivity.action = 3;
                            Log.d("TAG: ", "This should be song to artist map");
                            v.invalidate();
                        }

                        break;

                    case R.id.genresmap:
                        if (dragDataString.equals("SONG_MAP_BUTTON"))
                        {
                            HomeAcitivity.action = 2;
                            Log.d("TAG: ", "This should be song to genre map");
                            v.invalidate();
                        }
                        break;

                    }


                    // Returns true. DragEvent.getResult() will return true.
                    return(true);

                //break;

                case DragEvent.ACTION_DRAG_ENDED:

                    // Turns off any color tinting
                    ((ImageView) v).clearColorFilter();

                    // Invalidates the view to force a redraw
                    v.invalidate();

                    // Does a getResult(), and displays what happened.
                    if (event.getResult()) {
                        Log.d("End: ", "Well, end" + v.getId());
                        //Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG);

                    } else {
                        //Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG);

                    };

                    // returns true; the value is ignored.
                    return(true);

                //break;

                // An unknown action type was received.
                default:
                    Log.e("DragDrop Example","Unknown action type received by OnDragListener.");

                break;
        };
        return true;
    };

};

我想在startDrag之后,它会退出OnLongClick,你知道如何修复它吗?我添加了if v.startDrag…{//switch-case block},但是第一次长时间单击时,什么也没有发生,第二次单击时,最后一次长时间单击的结果显示出来。您是否解决了问题?