How can I create a custom container view in Android?

I would like to create a view with a listener that acts like a container or a grocery bag. Any image or text that is dragged into it is collected and stored in a local database. Rather, the file name or string is saved. I created custom views before displaying a custom data row, but I'm not sure how to create a grocery bag type. I searched Google to create a custom container view with a listener, but could not find anything related to what I was looking for. I am not asking anyone to do this for me, just give me advice or click in the right direction.

Edit Just clarify a little more. I already know how to drag a view. The problem is that you can go anywhere. What I want is the idea that when something falls out within its borders, it receives a line or a tag of representations. No matter what the view may be. Somehow this user view needs to know which view has been reset within its boundaries and delete that view when deleted.

+7
java android
source share
2 answers
<view class="at.calista.quatscha.views.SwipebarLayout" android:id="@+id/sbl" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- Top View --> <Button android:text="Top" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <!-- Bottom View --> <Button android:text="Bottom" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

+1
source share

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); //process data ((ViewGroup) v.getParent()).removeView(v); // to remove the item afterwards } } return false; } }); 
+1
source share

All Articles