2013-03-10 20 views
5

Ho alcuni frammenti che vengono aggiunti e rimossi da un codice RelativeLayout in modo dinamico utilizzando il codice. uno dei Fragments è un ListFragment, e come opposto ad altri miei frammenti che hanno un pulsante Chiudi e Salva questo contiene solo un elenco.Rimozione dei frammenti facendo clic/toccando all'esterno:

Il mio obiettivo è chiudere/rimuovere questo frammento facendo clic all'esterno di esso in qualsiasi punto dell'attività.

ho trovato il seguente codice:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // I only care if the event is an UP action 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
     // create a rect for storing the window rect 
     Rect r = new Rect(0, 0, 0, 0); 
     // retrieve the windows rect 
     this.getWindow().getDecorView().getHitRect(r); 
     // check if the event position is inside the window rect 
     boolean intersects = r.contains((int) event.getX(), (int) event.getY()); 
     // if the event is not inside then we can close the activity 
     if (!intersects) { 
      // close the activity 
      this.finish(); 
      // notify that we consumed this event 
      return true; 
     } 
    } 
    // let the system handle the event 
    return super.onTouchEvent(event); 
} 

che chiude un'attività non a tutto schermo quando si fa clic al di fuori di esso, ma solo che non sembrano capire come faccio a trovare il mio frammento rettangolo.

Qualcuno potrebbe aiutarmi e indicarmi la direzione giusta? Qualsiasi aiuto sarebbe apprezzato.

Grazie.

risposta

5

Beh, ho finalmente capito questo:

@Override 
public boolean onTouchEvent (MotionEvent event) 
{ 
    // I only care if the event is an UP action 
    if (event.getAction() == MotionEvent.ACTION_UP) 
    { 
     //and only is the ListFragment shown. 
     if (isListFragmentShown) 
     { 
      // create a rect for storing the fragment window rect 
      Rect r = new Rect (0, 0, 0, 0); 
      // retrieve the fragment's windows rect 
      currentFragment.getView().getHitRect(r); 
      // check if the event position is inside the window rect 
      boolean intersects = r.contains ((int) event.getX(), (int) event.getY()); 
      // if the event is not inside then we can close the fragment 
      if (!intersects) { 
       Log.d(TAG, "pressed outside the listFragment"); 
       FragmentTransaction fragmentTransaction; 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.remove(currentFragment).commit(); 
       // notify that we consumed this event 
       return true; 
      } 
     } 
    } 
    //let the system handle the event 
    return super.onTouchEvent (event); 
} 
1

È possibile aggiungere listener di clic al contenitore RelativeLayout. Se il frammento riguarda l'azione, attiva il listener di RelativeLayout in modo che l'ascoltatore funzioni solo mentre il frammento esiste.

+0

non ha veramente capito la tua risposta, hai letto la mia domanda fino alla fine? –