setOnLongClickListener无效

时间:2014-01-12 09:00:36

标签: android onlongclicklistener

我正在使用第三方库。它使图像可以拖动,并在捏合和旋转时缩放。我正在尝试删除图像,并制作了一种删除手指图像的方法。现在我只需要打电话给它。我成功调用它的唯一地方是onDraw。理想情况下,我会在doubleTap上调用它,因为用户将长按它来拖动它。但是longClick显然更容易,所以我首先测试它。这是我的代码。滚动到底部查看我的setOnClickListener。我将在setOnClickListener之前向您展示类代码,以便为您提供一些上下文:

public class PhotoSortrView extends View implements MultiTouchObjectCanvas<PhotoSortrView.Img> {

        private ArrayList<Integer> mDrawables = new ArrayList<Integer>();// { R.drawable.m74hubble, R.drawable.catarina, R.drawable.tahiti, R.drawable.sunset, R.drawable.lake };

        public void addDrawable(int drawable, Context context){
            Resources res = context.getResources();
            mDrawables.add(drawable);

            mImages.add(new Img(drawable, res));
            invalidate();
        }

        private ArrayList<Img> mImages = new ArrayList<Img>();
        private AppUser user;

        // --

        private MultiTouchController<Img> multiTouchController = new MultiTouchController<Img>(this);

        // --

        private PointInfo currTouchPoint = new PointInfo();

        private boolean mShowDebugInfo = true;

        private static final int UI_MODE_ROTATE = 1, UI_MODE_ANISOTROPIC_SCALE = 2;

        private int mUIMode = UI_MODE_ROTATE;

        // --

        private Paint mLinePaintTouchPointCircle = new Paint();

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

        public PhotoSortrView(Context context) {
            this(context, null);
        }

        public PhotoSortrView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public PhotoSortrView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }

        @SuppressWarnings("deprecation")
        private void init(Context context) {
            Resources res = context.getResources();
            for (int i = 0; i < mDrawables.size(); i++)
                mImages.add(new Img(mDrawables.get(i), res));

            mLinePaintTouchPointCircle.setColor(Color.YELLOW);
            mLinePaintTouchPointCircle.setStrokeWidth(5);
            mLinePaintTouchPointCircle.setStyle(Style.STROKE);
            mLinePaintTouchPointCircle.setAntiAlias(true);
            this.setLongClickable(true);

            this.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
                    Log.d("long clicking", "long clicking");
                    return true;
                }
            });
        }

我是Android新手,所以可能没有完全理解这个库类的上下文。我在我的活动中称这个课为:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dressing_room);
    photoSorter = new PhotoSortrView(this);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            this.addContentView(photoSorter, params);

它是一个添加到此活动的视图,但它有一个画布,可以添加许多可拖动的图像。谢谢。

编辑我将其更改为

this.setOnTouchListener(new View.OnTouchListener() {

               // @Override
                //public boolean onLongClick(View v) {
                //  Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
                //  Log.d("long clicking", "long clicking");
                //    return true;
               // }

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
                    Log.d("long clicking", "long clicking");
                    return false;
                }
            });
        }

这很有效。我们可以从那里得到doubleTap吗?

1 个答案:

答案 0 :(得分:2)

内置GestureDetector能够“捕捉”设备与用户之间的所有常见互动,包括双击。但是,要为单个视图或一组视图设置它,您必须做一些繁琐的工作。我现在将发布我的测试活动的代码:

public class MainActivity extends Activity implements OnClickListener {
    private GestureDetector gd;
    View.OnTouchListener otl;

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
            return true;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gd = new GestureDetector(this, new MyGestureDetector());
        otl = new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return gd.onTouchEvent(event);
            }
        };
        LinearLayout cotainerView = (LinearLayout) findViewById(R.id.container);
        TextView tvNormal = new TextView(this);
        tvNormal.setText("Normal text view");
        tvNormal.setHeight(50);
        DoubleTapTextView tvDoubleTap = new DoubleTapTextView(this);
        tvDoubleTap.setText("Double tap text view");
        tvDoubleTap.setHeight(50);
        tvDoubleTap.setOnClickListener(this);
        tvDoubleTap.setOnTouchListener(otl);
        containerView.addView(tvNormal);
        containerView.addView(tvDoubleTap);
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

所以诀窍是将onTouch事件及其参数(MotionEvent e)传递给您的自定义GestureListener。这样,每个Gesture(Touch事件)都将被监听,但是您的自定义侦听器将仅在双击手势的情况下执行onDoubleTap(MotionEvent e)方法。希望你能理解这背后的想法,作为一种享受,我发布了一个在模拟器中运行的应用程序的屏幕截图,向您展示该事件实际上已被捕获:) Screenshot

编辑:以下是解决方案的要点

  1. Activity implements OnClickListener
  2. OnClickListerOnTouchListener

    注册所需的视图
    tvDoubleTap.setOnClickListener(this); //Your activity implements OnClickListener
    tvDoubleTap.setOnTouchListener(otl); //The wrapper listener from which you call your custom one
    
  3. 在基本的OnTouchListener中,将参数传递给自定义GestureListener

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
    
  4. 在自定义GestureListener的onDoubleTap(MotionEvent e)方法中,实施您的解决方案。

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    
  5. 致谢:感谢 gav ,感谢他对his question的研究