为什么 onClick 不触发拖动侦听器,但长按可以正常工作?[android]

Why does onClick does not fire drag listener, but on Long click works fine?[android]

我在屏幕上显示了 apple,我有 2 个听众:onClickListeneronDragListener。简单点击苹果图片不会触发拖动,但长按可以。

package nis.drag_apple;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

ImageView square ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView apple = (ImageView)findViewById(R.id.apple);
    square = (ImageView)findViewById(R.id.square);

    apple.setTag("apple");
    square.setTag("square");

    apple.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View clicked_apple) {
            Toast.makeText(getApplicationContext(), " on click apple!", Toast.LENGTH_SHORT).show();
            ClipData.Item item = new ClipData.Item((CharSequence)clicked_apple.getTag());
            String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
            ClipData data = new ClipData(clicked_apple.getTag().toString(), mimeTypes, item);
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(clicked_apple);

            clicked_apple.startDrag(data, //data to be dragged
                    shadowBuilder, //drag shadow
                    clicked_apple, //local data about the drag and drop operation
                    0   //no needed flags
            );
            return true ;
        }
    });


    apple.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction() ) {

                //signal for the start of a drag and drop operation.
                case DragEvent.ACTION_DRAG_STARTED:

                    Toast.makeText(getApplicationContext(), "DRAG STARTED!", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "the tag is " + v.getTag().toString(), Toast.LENGTH_SHORT).show();
                    // do nothing
                    break;

                //the drag point has entered the bounding box of the View
                case DragEvent.ACTION_DRAG_ENTERED:

                    break;


                case DragEvent.ACTION_DRAG_EXITED:

                    break;

                case DragEvent.ACTION_DROP:
                    Toast.makeText(getApplicationContext(), "action DROP!", Toast.LENGTH_SHORT).show();
                    if(v == square) {
                        View image_moved = (View) event.getLocalState();
                        ViewGroup parent = (ViewGroup)     
                        image_moved.getParent();
                        parent.removeView(image_moved);

                      //  image_moved.setVisibility(View.VISIBLE);
                    } else {
                        View view = (View) event.getLocalState();
                        view.setVisibility(View.VISIBLE);
                        Context context = getApplicationContext();

                        break;
                    }
                case DragEvent.ACTION_DRAG_ENDED:

                default:
                    break;
            }
            return true;
        }
    });
   }

}

要发生点击事件,您必须抬起手指 (ACTION_UP),这对于启动拖动操作没有意义。另一方面,长按发生在您的手指仍在屏幕上的情况下。 (更合适的名称可能是 "long press"。)

如果您想在手指第一次接触视图时立即开始拖动,可以在 ACTION_DOWN 事件发生时使用 OnTouchListener 来实现。