Drag View from Activity and drop it in new Fragment android

戏子无情 提交于 2020-01-24 20:57:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!