How can you determine what kind of view you are viewing when performing a touch event?

I want to know how I can define child views if I move a view from one ViewGroup to another ViewGroup, especially when a touch event occurs. Is there a way that I can name that will let me know what kind of looks I'm β€œsoaring”?

What I am doing now is when I detect the ACTION_MOVE event in my view, I raise it to the top-level parent so that it can move and draw inside the whole window (and not just inside the original parent), then I want to move the view to another ViewGroup, and in ACTION_UP attach the view to this ViewGroup.

+3
source share
4 answers

I found that Sebastian Roth answered very helpful with resources, but since that was not my answer, I thought I would share what I came up with.

Here is the code that I use to detect views (only those that will accept drops) with a coordinate on the screen.

            private DropView findDropTarget( int x, int y, int[] dropCoordinates ){
                final Rect r = mRectTemp;
                final ArrayList<DropView> dropTargets = ((main) context).getBoardDropTargets();
                final int count = dropTargets.size();
                for (int i=count-1; i>=0; i--) {
                    final DropView target = dropTargets.get(i);
                    target.getHitRect(r);
                    target.getLocationOnScreen(dropCoordinates);
                    r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
                    if (r.contains(x, y)) {
                        dropCoordinates[0] = x - dropCoordinates[0];
                        dropCoordinates[1] = y - dropCoordinates[1];
                        return target;
                    }
                }
           }

Ok, first mRectTemp is just the highlighted Rectangle, so you don't need to create new ones (IE final Rect r = new Rect ())

The next line of dropTargets is a list of views that will accept a drop in my application. Then I look at each view.

Then I use getHitRect (r) to return the coordinates of the view screen.

Then I compensate the coordinates to account for the notification bar or any other view that could shift the coordinates.

, , x y r (x y - event.rawX() event.rawY()).

, , .

+3

, , MotionEvent # getX()/getY() View # getTop()/etc , , , ViewGroups:

        private boolean inRegion(float x, float y, View v) {
            v.getLocationOnScreen(mCoordBuffer);
            return mCoordBuffer[0] + v.getWidth() > x &&    // right edge
                   mCoordBuffer[1] + v.getHeight() > y &&   // bottom edge
                   mCoordBuffer[0] < x &&                   // left edge
                   mCoordBuffer[1] < y;                     // top edge
        }

OnTouchListener , :

        boolean inside = inRegion(event.getRawX(), event.getRawY(), targetView);
+5

, .

  • ArrayList .
  • , .
private View findView(float x, float y, ArrayList<View> targets)
{
    final int count = targets.size();
    for (int i = 0; i < count; i++) {
        final View target = targets.get(i);
        if (target.getRight() > x && target.getTop() < y
                && target.getBottom() > y && target.getLeft() < x) {
            return target;
        }
    }
    return null;
}
+4

Source: https://habr.com/ru/post/1777833/


All Articles