Billda
Billda

Reputation: 5987

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.

Upvotes: 6

Views: 3699

Answers (3)

MyrionSC2
MyrionSC2

Reputation: 1318

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.

Upvotes: 7

Corneliu Dascălu
Corneliu Dascălu

Reputation: 3958

If you return true in onLongClick() it means the callback has consumed the event and thus it is not propagated further. If you return false, then it will reach down to the child views. (I assume the touch listener is set on the shadow ImageView).

Upvotes: 1

Stephen
Stephen

Reputation: 762

I believe to delegate try returning false

Upvotes: 0

Related Questions