我为我的英语道歉。我有个问题。 我使用听众LongClick和TouchListener。
在长按中 - 视图将被删除,父视图将插入另一个父视图中。 触摸停止工作,因为父改变了。
如何将TouchListener保持为View?
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);
}
答案 0 :(得分:0)
当您要添加视图 Main.mainRelative.addView(v);
时
在创建新视图之前,使用此视图初始化 v ,如查看视频= v
然后再次设置触摸侦听器 vv.setOnTouchLIstener(ot1);
试试这个应该有用......