为什么onClick不会激活拖拽监听器,但是长时间点击工作正常?[android]

时间:2015-06-02 05:22:36

标签: java android onclicklistener onlongclicklistener

我屏幕上有苹果,我有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;
        }
    });
   }

}

1 个答案:

答案 0 :(得分:3)

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

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