限制用户将视图拖出Android拖放中的布局

时间:2019-06-17 17:00:10

标签: android layout view drag out

我有一个OnDragListener,我通过Onlongclick视图在此侦听器上放置了视图,但是要解决的问题是我想将视图限制在布局视图之外,可以吗?对于视图的不同参数(根据可拖动对象的长度和宽度的不同),甚至可以将其放置在布局的外面一点点 当视图在拖动侦听器布局上拖动时,也试图在输入框中获取当前运行时坐标x y的位置

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final ImageView imgVW = (ImageView) findViewById(R.id.imageView);
    final TextView txtVW = (TextView) findViewById(R.id.textView);





    txtVW.setOnLongClickListener(this);

    imgVW.setOnLongClickListener(this);

    findViewById(R.id.layout3).setOnDragListener(this);




}

@Override
public boolean onDrag(View v, DragEvent event) {

    int action = event.getAction();

    switch (action) {

        case DragEvent.ACTION_DRAG_STARTED:

            if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                return true;
            }

            return false;

        case DragEvent.ACTION_DRAG_ENTERED:

            v.invalidate();
            return true;

        case DragEvent.ACTION_DRAG_LOCATION:

            return true;

        case DragEvent.ACTION_DRAG_EXITED:

            v.invalidate();
            return true;

        case DragEvent.ACTION_DROP:

            ClipData.Item item = event.getClipData().getItemAt(0);

            String dragData = item.getText().toString();

            Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_SHORT).show();



            float X=event.getX();
            float Y=event.getY();
            v.invalidate();


            View vw = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) vw.getParent();
            owner.removeView(vw);

            RelativeLayout container = (RelativeLayout) v;


            vw.setX(X-(vw.getWidth()/2));
            vw.setY(Y-(vw.getHeight()/2));
            container.addView(vw);


            return true;

        case DragEvent.ACTION_DRAG_ENDED:

            v.invalidate();

            if (event.getResult()) {

                Toast.makeText(this, "The drop was handled.", Toast.LENGTH_SHORT).show();

            }

            else
                Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_SHORT).show();

            return true;
        default:
            Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
            break;
    }

    return true;
}

@Override
public boolean onLongClick(View v) {

    ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());

    String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
    ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);

    View.DragShadowBuilder dragshadow = new View.DragShadowBuilder(v);

    v.startDrag(data
            , dragshadow
            , v
            , 0
    );



    return true;
}

dont want to be placed out of layout like this

enter image description here

I want user to place views full inside the layout like this

enter image description here

1 个答案:

答案 0 :(得分:0)

const getLocalitation = async () =>{
    console.log('DENTRO DE GetLocalitaion');

    const geoOptions={
        enableHighAccuracy: true,
        timeOut: 10000
    };

    const coordenates =  navigator.geolocation.getCurrentPosition( geoSucces,goFailure, geoOptions);
    console.log('DESPUES DE COORDENATES');

    return coordenates;
} 

检查X拖动点和视图长度的总和是否超过拖动容器的长度。 如果是,则从拖动容器的宽度中减去视图宽度以获取X点,这将限制某些零件视图放置在拖动容器的外部。否则,拖动X点应足以将视图放置在拖动容器中。

        RelativeLayout container = (RelativeLayout) v;

与使用两个视图的高度数据的Y约束相同。

            if((X+vw.getWidth())>container.getWidth())
              vw.setX(container.getWidth()-vw.getWidth());
            else
              vw.setX(X);

您可以根据需要设置其他条件。