Skip to content Skip to sidebar Skip to footer

How To Register A Dragevent While Already Inside One And Have It Listen In The Current Dragevent?

My question is is related to the Drag and Drop API in Android. I apologize if my question was confusing. But essentially, when you want to start a Drag and Drop operation, you must

Solution 1:

I know this is an old post but if somebody needs a solution/hack... I had struggled with this for a couple of hours and thought that it might be useful for someone.

My problem

I got this problem when I was trying to have my ListView scroll up/down while I was dragging an item. The issue was that when scrolling, the listview would load new converted views to the group view and the ones that were not active while the drag started (they were in the listview cache - and not associated as children for the view group) did not respond to the dragging.

You had the right idea about it, because the view missed the initial DRAG_STARTED event then it wasn't getting any other events.

I was going through the source code for VIEW/VIEW GROUP and there are a few ways that we can hack this thing. Eventually I decided to solve it by forcing a visibility change for the problematic view, which will start a chain of events that will eventually include the view in future DRAG EVENTS calls

Answer : visibility change hack

So after you have the new view attached to the parent simply set visibility to GONE and back to VISIBLE:

child.setVisibility(View.GONE);
child.setVisibility(View.VISIBLE);

Note: do this while a) the user is still dragging b) the new view is already added to a parent.

Note: For me this was enough to solve the problem. BUT, in my case I had other views in the same container that were already accepting DRAG EVENTS. If this is the first child in that container it might not work. You can try to do the same trick to the container ,and to the container's container, and so on until you reach a container that had children that accept the EVENTS.

Solution 2:

Old question, but I was trying to do something very similar. A cleaner approach that worked for me was:

  1. Attach the DragListener to the ExpandableListView instead of the individual views within it.

  2. Then based on the x,y coordinated of DragEvent.ACTION_DRAG_LOCATION event, identify group or child view and do the required action.

This way the group/child view doesn't have to be visible at the time drag event started.

Post a Comment for "How To Register A Dragevent While Already Inside One And Have It Listen In The Current Dragevent?"