【问题标题】:Touch listener and changing parent触摸侦听器和更改父级
【发布时间】:2013-11-15 12:36:27
【问题描述】:

我为我的英语道歉。我有个问题。 我使用监听器 LongClick 和 TouchListener。

在长按中 - 视图被移除,父视图被插入到另一个父视图中。 触摸停止工作,因为父级发生了变化。

如何让 TouchListener 保持查看状态?

static boolean drag = false;
static View parent;
static OnLongClickListener olcl = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {

        if(!drag) {
            parent = (View) v.getParent();
            int[] loc = {0, 0};
            v.getLocationOnScreen(loc);
            ((ViewGroup) parent).removeView(v);
            Main.mainRelative.addView(v);

            RelativeLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
            par.leftMargin = loc[0];
            par.topMargin = loc[1];
            v.setLayoutParams(par);

            drag = true;
            return true;
        }
        return false;
    }
};

static OnTouchListener otl = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(v.getParent() != null && v.getParent() == Main.mainRelative) {
            //!!!!!!!!!!!!!
            RelativeLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
            par.leftMargin = (int) (event.getRawX() - v.getWidth()/2);
            par.topMargin = (int) (event.getRawY() - v.getHeight()/1.2);
            v.setLayoutParams(par);

            for(int y = 0; y < Main.radioGroupLeftMain.getChildCount(); y++) {
                RadioButton rb = (RadioButton) ((ViewGroup) Main.radioGroupLeftMain.getChildAt(y)).getChildAt(0);
                if(checkHit((int)event.getRawX(), (int)event.getRawY(), rb)) {
                    rb.setChecked(true);
                } else {
                    rb.setChecked(false);
                }

                if(event.getAction() == MotionEvent.ACTION_UP) {


                    try { ((ViewGroup) v.getParent()).removeView(v); } catch(Exception e) {};
                    if(v.getVisibility() == View.VISIBLE) {
                        ((ViewGroup) parent).addView(v);
                    }
                    drag = false;
                }
            }   
        }



        return false;
    }
};

public static boolean checkHit(int x, int y, View v) {
    int[] loc = {0, 0};
    v.getLocationOnScreen(loc);
    Rect rv = new Rect(loc[0], loc[1], loc[0]+v.getWidth(), loc[1]+v.getHeight());


    return rv.contains(x, y);
}

【问题讨论】:

    标签: java android drag-and-drop ontouchlistener onlongclicklistener


    【解决方案1】:

    当你要添加视图时Main.mainRelative.addView(v);
    在此创建新视图之前,使用此视图 v 进行初始化,例如 View vv = v
    然后再次设置触摸监听器 vv.setOnTouchLIsener(ot1);
    试试这个应该可以的...

    【讨论】:

    • 感谢您的回复。我已经尝试过了。这不起作用,因为它不起作用 ACTION_DOWN
    • “不起作用ACTION_DOWN”是什么意思??你想要点击监听器或触摸监听器或两者兼而有之?
    • View 有 onLongClickListener 和 onTouchListener。当您第一次单击时会触发 longClick,并且 TouchListener 不起作用。如果您从屏幕上松开手指并重新应用 - 工作 ACTION_DOWN 视图并开始移动您的手指
    • 对不起,ACTION_DOWN 工作。但在 longClick 之后 - TouchListener 尚未运行。
    • hmm...你能做一件事吗...在 ACTION_UP 中做你的工作,假设如果发生长按,那么你的 ACTION_UP 在这种情况下将不起作用,你可以在长按中处理你可以使用的功能还有一个标志变量....
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多