Android:允许TextView在RelativeLayout之外拖动

时间:2013-06-05 19:30:29

标签: android drag-and-drop textview imageview drag

我创建了一个OnTouchListener,它允许我在屏幕上拖动视图。这很有效,除了一个问题:

我的观点调整大小而不是离开父级。他们在RelativeLayout中,我怀疑这是导致问题的原因。它们仅在触摸RelativeLayout的右边界或底边界时调整大小。

我希望这个问题有一个简单的解决方案,(理想情况下不涉及调整RelativeLayout的大小),但我还没有找到解决方案。

理想情况下,此解决方案适用于所有视图,因为我将此OnTouchListener用于TextViews以及ImageViews。我希望找到一个XML解决方案。我假设其他人已经处理过这个问题了?

PS:在我发布之前......我记得我相信有一个用于击中RelativeLayout边框的标志,所以也许我可以在发生这种情况时对TextView应用负边距。想法?

修改

这是我的OnTouchListener:

// Dragging functionality for all views and double tap to remove
// functionality for ImageViews
final OnTouchListener onTouchListener = new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        v.bringToFront(); 

        //if(v instanceof android.widget.ImageView) 
            //mScaleDetector.onTouchEvent(event);

        layoutParams = (LayoutParams) v.getLayoutParams();

        switch (event.getActionMasked())
        {
        case MotionEvent.ACTION_DOWN:
            // Where the user started the drag
            pressed_x = (int) event.getRawX();
            pressed_y = (int) event.getRawY();

            if (v instanceof android.widget.ImageView)
            {
                curTime = System.currentTimeMillis();
                if(curTime - prevTime <= DOUBLE_TAP_INTERVAL)
                {
                    v.setVisibility(View.GONE);
                }
                prevTime = curTime;
            }
        case MotionEvent.ACTION_MOVE:
            // Where the user's finger is during the drag
            final int x = (int) event.getRawX();
            final int y = (int) event.getRawY();

            // Calculate change in x and change in y
            dx = x - pressed_x;
            dy = y - pressed_y;

            // Update the margins
            layoutParams.leftMargin += dx;
            layoutParams.topMargin += dy;
            v.setLayoutParams(layoutParams);

            // Save where the user's finger was for the next ACTION_MOVE
            pressed_x = x;
            pressed_y = y;
            break;
        default:
            break;
        }
        return true;
    }
}

0 个答案:

没有答案