如何通过拖放在布局中保留textview?

时间:2015-06-28 10:07:31

标签: android drag-and-drop

我尝试在android中使用拖放 API。我在var watch = require('gulp-watch'); // Styles gulp.task('styles', function() { gulp.src('./_themes/blanktheme/ui/scss/styles.scss') .pipe(include()) .pipe(sass({ errLogToConsole: true })) .pipe(minifycss()) .pipe(gulp.dest('./_themes/blanktheme/ui/css/')) }); // Watch gulp.task('watch', function() { watch('./_themes/blanktheme/ui/scss/*.scss', function () { gulp.start('styles'); }); }); gulp.task('default', ['styles', 'watch']); 中有一个textview。我希望将此linearlayout保留在布局中。当您拖动此textview时,我会阻止layout

我该怎么做?我只想拖动textview而不是vertically。我可以完全拖放我想要的东西。当我拖动时,发生溢出。 我的代码:

overflow

我想避免 public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); view.startDrag(null, shadowBuilder, view, 0); view.setVisibility(View.INVISIBLE); return true; } else { return false; } } public boolean onDrag(View layoutview, DragEvent dragevent) { int action = dragevent.getAction(); View view = (View) dragevent.getLocalState(); int x=(int) dragevent.getX(); int y=(int) dragevent.getY(); if (x<0) { view.setX(0); } if (y<0) { view.setY(0); y=0; } if (y>0) { switch (action) { case DragEvent.ACTION_DRAG_STARTED: Log.d(LOGCAT, "Drag event started"); break; case DragEvent.ACTION_DRAG_ENTERED: Log.d(LOGCAT, "Drag event entered into " + layoutview.toString()); break; case DragEvent.ACTION_DRAG_EXITED: Log.d(LOGCAT, "Drag event exited from " + layoutview.toString()); break; case DragEvent.ACTION_DROP: Log.d(LOGCAT, "Dropped"); ViewGroup owner = (ViewGroup) view.getParent(); owner.removeView(view); //only dragging vertically view.setY(dragevent.getY()); LinearLayout container = (LinearLayout) layoutview; container.addView(view); view.setVisibility(View.VISIBLE); break; case DragEvent.ACTION_DRAG_ENDED: Log.d(LOGCAT, "Drag ended"); break; default: break; } } return true; } dragginglayout宽度为TextView,因此只能垂直拖动。 我怎样才能做到这一点 ?

提前致谢。

1 个答案:

答案 0 :(得分:0)

从不同的答案中查看此代码

 public class OnDragTouchListener implements View.OnTouchListener {

/**
 * Callback used to indicate when the drag is finished
 */
public interface OnDragActionListener {
    /**
     * Called when drag event is started
     *
     * @param view The view dragged
     */
    void onDragStart(View view);

    /**
     * Called when drag event is completed
     *
     * @param view The view dragged
     */
    void onDragEnd(View view);
}

private View mView;
private View mParent;
private boolean isDragging;
private boolean isInitialized = false;

private int width;
private float xWhenAttached;
private float maxLeft;
private float maxRight;
private float dX;

private int height;
private float yWhenAttached;
private float maxTop;
private float maxBottom;
private float dY;

private OnDragActionListener mOnDragActionListener;

public OnDragTouchListener(View view) {
    this(view, (View) view.getParent(), null);
}

public OnDragTouchListener(View view, View parent) {
    this(view, parent, null);
}

public OnDragTouchListener(View view, OnDragActionListener onDragActionListener) {
    this(view, (View) view.getParent(), onDragActionListener);
}

public OnDragTouchListener(View view, View parent, OnDragActionListener onDragActionListener) {
    initListener(view, parent);
    setOnDragActionListener(onDragActionListener);
}

public void setOnDragActionListener(OnDragActionListener onDragActionListener) {
    mOnDragActionListener = onDragActionListener;
}

public void initListener(View view, View parent) {
    mView = view;
    mParent = parent;
    isDragging = false;
    isInitialized = false;
}

public void updateBounds() {
    updateViewBounds();
    updateParentBounds();
    isInitialized = true;
}

public void updateViewBounds() {
    width = mView.getWidth();
    xWhenAttached = mView.getX();
    dX = 0;

    height = mView.getHeight();
    yWhenAttached = mView.getY();
    dY = 0;
}

public void updateParentBounds() {
    maxLeft = 0;
    maxRight = maxLeft + mParent.getWidth();

    maxTop = 0;
    maxBottom = maxTop + mParent.getHeight();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (isDragging) {
        float[] bounds = new float[4];
        // LEFT
        bounds[0] = event.getRawX() + dX;
        if (bounds[0] < maxLeft) {
            bounds[0] = maxLeft;
        }
        // RIGHT
        bounds[2] = bounds[0] + width;
        if (bounds[2] > maxRight) {
            bounds[2] = maxRight;
            bounds[0] = bounds[2] - width;
        }
        // TOP
        bounds[1] = event.getRawY() + dY;
        if (bounds[1] < maxTop) {
            bounds[1] = maxTop;
        }
        // BOTTOM
        bounds[3] = bounds[1] + height;
        if (bounds[3] > maxBottom) {
            bounds[3] = maxBottom;
            bounds[1] = bounds[3] - height;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                onDragFinish();
                break;
            case MotionEvent.ACTION_MOVE:
                mView.animate().x(bounds[0]).y(bounds[1]).setDuration(0).start();
                break;
        }
        return true;
    } else {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDragging = true;
                if (!isInitialized) {
                    updateBounds();
                }
                dX = v.getX() - event.getRawX();
                dY = v.getY() - event.getRawY();
                if (mOnDragActionListener != null) {
                    mOnDragActionListener.onDragStart(mView);
                }
                return true;
        }
    }
    return false;
}

private void onDragFinish() {
    if (mOnDragActionListener != null) {
        mOnDragActionListener.onDragEnd(mView);
    }

    dX = 0;
    dY = 0;
    isDragging = false;
}
  }

您可以使用以下方式进行设置:

myView.setOnTouchListener(new OnDragTouchListener(myView));

或者直接在自定义视图的init方法中添加:

setOnTouchListener(new OnDragTouchListener(this));