You will need a method that determines if two views overlap. For example, something like these methods might work when the target view and the dragged item are two different views in the same container view:
private Rect getScreenBounds(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); return new Rect(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight()); } private boolean doViewsIntersect(View dropTarget, View item) { Rect dropRect = getScreenBounds(dropTarget); Rect itemRect = getScreenBounds(item); return Rect.intersects(dropRect, itemRect); }
However, if the drag-and-drop views are child views in the container, and this container has a special hit area in which you want to execute the reset logic, then you can simply use this Rect hit area for the intersection test.
To save View information, you have several options:
- You can use
view.setTag(key, object) and view.getTag(key) to store any information that you need for each element. - If you use a custom class for elements, you can simply add a method to get data from the presentation class.
- If you use several different classes for elements, you can create an interface and implement this interface for each drag and drop view class.
In the itemβs release, you can check if regions / views intersect, and if they do, get the necessary information from the dragged item for processing as needed, using getTag or a method added to the view,
For example, this can be called using onTouchListener when you receive the MotionEvent.ACTION_UP event. Here is an example listener that you can add to each element:
item.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getActionMasked() == MotionEvent.ACTION_UP) { if (doViewsIntesect(dropTarget, v)) { Object data = v.getTag(DATA_KEY);
Allen g
source share