I apologize for my English. I have a problem. I use a listeners LongClick and TouchListener.
In the long click - the view is removed and the parent view is inserted into the other parent. Touch stops working, because there was a change parent.
How do I keep TouchListener to 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);
}
when your are going to add view Main.mainRelative.addView(v);
before this create new View, initialize with this view v like View vv = v
then again set touch listener vv.setOnTouchLIstener(ot1);
try this it should work...