问题
i try to implement a Drag and Drop in my app, I drag the view from my Activity, and i want drop it in new Fragment created in the moment when drag start, but don't work the Drag Listener in the Fragment, i use the next code in my activity:
findViewById(R.id.button1).setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
vibra();
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
FragmentShare share = new FragmentShare(getSupportFragmentManager());
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.principal, share);
ft.addToBackStack(null);
ft.commit();
return false;
}
});
And the code in my fragment is the next:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vv = inflater.inflate(R.layout.fragment_share, container, false);
vv.findViewById(R.id.button1).setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setEnabled(false);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setEnabled(true);
break;
case DragEvent.ACTION_DROP:
manager.popBackStackImmediate();
break;
case DragEvent.ACTION_DRAG_ENDED:
manager.popBackStackImmediate();
break;
default:
break;
}
return true;
}
});
return vv;
}
But the view state never change and don't back when drop the view. I don't know if this its possible or not, or have other way to implement this please tell me what is this.
Thanks.
Pd. Sorry for my english.
回答1:
If your application's min sdk version is 11, then this works,
We have getters and setters for X,Y coordinates , width and height of a view. Using these methods, we can move a view all over the screen.
So, what i do to accomplish your task is,
1) I start updating x position of a view in onTouchListener of the view. 2) As soon as i start, simultaneously i create a fragment. 3) When my view reaches the fragment i will finish() the activity and add the same layout to the fragment.
In this way we can illute.
Hope this helps..
来源:https://stackoverflow.com/questions/16990256/drag-view-from-activity-and-drop-it-in-new-fragment-android