【问题标题】:Why does onClick does not fire drag listener, but on Long click works fine?[android]为什么 onClick 不会触发拖动侦听器,但在长按上可以正常工作?[android]
【发布时间】:2015-06-02 05:22:36
【问题描述】:

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

}

【问题讨论】:

    标签: java android onclicklistener onlongclicklistener


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多