Puoi usa il codice qui sotto per ottenere la tua richiesta:
Metodo per testare vista limiti (utilizzato nel codice beloow)
Rect outRect = new Rect();
int[] location = new int[2];
private boolean isViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
Test con due TextView
final TextView viewA = (TextView) findViewById(R.id.textView);
final TextView viewB = (TextView) findViewById(R.id.textView2);
Setup viewA
Il vuoto OnClickListener
è tenuto a mantieni OnTouchListener
attivo fino ACTION_UP
viewA.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {}
});
viewA.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_UP){
if(isViewInBounds(viewB, x, y))
viewB.dispatchTouchEvent(event);
else if(isViewInBounds(viewA, x, y)){
Log.d(TAG, "onTouch ViewA");
//Here goes code to execute on onTouch ViewA
}
}
// Further touch is not handled
return false;
}
});
Setup viewB
Questa operazione è necessaria solo se si desidera anche di premere sul viewB e trascinare per viewA
viewB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {}
});
viewB.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_UP){
if(isViewInBounds(viewA, x, y))
viewA.dispatchTouchEvent(event);
else if(isViewInBounds(viewB, x, y)){
Log.d(TAG, "onTouch ViewB");
//Here goes code to execute on onTouch ViewB
}
}
// Further touch is not handled
return false;
}
});
saluti.
[VEDERE QUESTO ANCHE] (http://stackoverflow.com/questions/13309111/programatically-execute-another-touch-event-when-to-touch-event-occures-in-andr) –