将RelativeLayout移出屏幕(使用onTouch)

时间:2013-08-30 12:02:43

标签: java android

我有一个RelativeLayout,我希望用户通过触摸来移动它。我希望RelativeLayout移出屏幕(不调整大小,但隐藏它)。然后我尝试了这段代码:

// onTouch method:
boolean hasMoved = false;
ViewConfiguration vc = ViewConfiguration.get(context);
int slop = vc.getScaledTouchSlop();
float touch, move, delta;
if (event.getAction() == MotionEvent.ACTION_DOWN) {

    touch = event.getX();
    return true;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {

    move = event.getX();
    delta = touch - move;
    if(Math.abs(delta) > slop) hasMoved = true;
    if (hasMoved) {

        int lMargin = (int) ((touch - move) * (-1));
        LayoutParams lp = (LayoutParams) relativeLayout.getLayoutParams();
                ((MarginLayoutParams) lp).leftMargin = lMargin;
                relativeLayout.setLayoutParams(lp);
    }
}

但它不能正常工作。 relativeLayout会闪烁(边距变为disired position,然后在用户移动时多次返回原始位置)。

抱歉我的英语不好。如果您愿意,我可以尝试更好地解释或以其他方式解释。

谢谢!

1 个答案:

答案 0 :(得分:0)

不确定我是否正确理解您要正确执行的操作,但要隐藏视图,您可以执行此类操作。

relativeLayout.SetVisibility(View.GONE)

relativeLayout.SetVisibility(View.INVISIBLE)