How to invoke drag event of a custom view inside S

2019-07-25 08:31发布

How can i drag a custom view inside the SurfaceView. Touch events are not dispatched to child custom view from SurfaceView or child view's onTouchEvent is not called.

1条回答
Root(大扎)
2楼-- · 2019-07-25 09:32

Try this code

 public class MoveViewTouchListener implements View.OnTouchListener
{
    private GestureDetector mGestureDetector;
    private View mView;


    public MoveViewTouchListener(View view)
    {
        mGestureDetector = new GestureDetector(view.getContext(), mGestureListener);
        mView = view;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        return mGestureDetector.onTouchEvent(event);
    }

    private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener()
    {
        private float mMotionDownX, mMotionDownY;

        @Override
        public boolean onDown(MotionEvent e)
        {
            mMotionDownX = e.getRawX() - mView.getTranslationX();
            mMotionDownY = e.getRawY() - mView.getTranslationY();
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            mView.setTranslationX(e2.getRawX() - mMotionDownX);
            mView.setTranslationY(e2.getRawY() - mMotionDownY);
            return true;
        }
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (g) {
                if (groupselecteditems.contains(mView)) {
                    mView.setBackgroundColor(Color.TRANSPARENT);
                    groupselecteditems.remove(mView);
                } else {
                    mView.setBackgroundResource(border);
                    groupselecteditems.add((MyTextView) mView);
                }
            } else {
                toucheditem = mView;
                xcountry = mView.getX();
                ycountry = mView.getY();
            }
            return true;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            if (g) {
                if (groupselecteditems.size() > 0) {
                    editGroup();
                }
            } else {
                changtextfeatures((TextView) mView);
            }
        }
    };
}

Call this class with your view

 view.setOnTouchListener(new MoveViewTouchListener(view));
查看更多
登录 后发表回答