Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate touch event from one view to another

I am trying to implement my own drag and drop with touch view events. I want to trigger dragging with long click, in onLongClick i create view shadow as bitmap and this bitmap is set to imageview. This imageview i want to drag. My problem is, that imageview is not responding to touch events immediately after that long click event. I have to stop touching screen and tap to imageview again and then my image is moving.

Some relevant code:

  mCategoryNews.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                v.setVisibility(View.INVISIBLE);
                ImageView shadow = (ImageView) getView().findViewById(R.id.imgViewShadow);
                shadow.setLayoutParams(new FrameLayout.LayoutParams(v.getWidth(), v.getHeight()));
                shadow.setImageBitmap(Utils.loadBitmapFromView(v));
                shadow.bringToFront();
                ((FrameLayout.LayoutParams) shadow.getLayoutParams()).leftMargin = rowCategories1.getLeft();
                ((FrameLayout.LayoutParams) shadow.getLayoutParams()).topMargin = rowCategories1.getTop();

                return true;
            }

        });

private View.OnTouchListener mDragShadowTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "onTouch");
            switch (event.getAction()) {

                case MotionEvent.ACTION_MOVE:
                    Log.d(TAG, "action move");
                    int x = (int) event.getRawX();//- rowCategories1.getLeft() - v.getWidth() / 2;
                    int y = (int) event.getRawY();//- rowCategories1.getTop() - v.getHeight();
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(mRowWidth / 2, mRowHeight);
                    params.setMargins(x, y, 0, 0);
                    v.setLayoutParams(params);

                    break;
                case MotionEvent.ACTION_DOWN:
                    break;
            }
            return true;
        }
    };

no log output is present while i am still holding finger on screen after long tap.

like image 732
Billda Avatar asked Apr 22 '14 11:04

Billda


1 Answers

This is a late answer, but I had a similar problem and found this solution:

Pass the MotionEvent to the view that should take it over via the dispatchTouchEvent method.

View myView = findViewById(R.id.myView);
@Override
public boolean onTouchEvent(MotionEvent event) {
    myView.dispatchTouchEvent(event);
}

After the MotionEvent is passed, myView takes over and responds to new touch events.

like image 159
MyrionSC2 Avatar answered Sep 28 '22 02:09

MyrionSC2