2011-07-18 13 views
34

Estoy trabajando con un ListFragment y haciendo un onListItemClick. Todo funciona bien, pero ahora quiero usar un largo artículo Haga clic en (por ejemplo setOnItemLongClickListener (nueva OnItemLongClickListener() para realizar una actividad). ¿Cómo puedo usar esto en mi fragmento?Clic largo en ListFragment

Gracias!

+3

Encontré el camino: getListView(). SetOnItemLongClickListener (nueva OnItemLongClickListener() { \t \t \t @ Override \t \t \t onItemLongClick pública booleano (AdapterView paramAdapterView, \t \t \t Ver paramView, posición int, long paramLong) {// TODO return true; } }); – tsync

Respuesta

58

Sí, la solución . publicado por Tsync funciona para mí yo también había encontró con el mismo problema y consideró que esto no es posible probé la sugerencia anterior de la siguiente manera:.

public class ProjectsFragment extends ListFragment { 

@Override 
public void onActivityCreated(Bundle savedState) { 
    super.onActivityCreated(savedState); 

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show(); 
      return true; 
     } 
    }); 

y funcionó

+0

y funcionó para mí también. – danny117

+1

¿Importa si devolvemos verdadero o falso? @Narayanan – Manny265

+1

@ Manny265 return false ejecutará el oyente onclick, return true previene esto –

11

esto funciona para mí

!
getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 
    public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) { 
     //Get your item here with the position     
     return true; 
    } 
}); 
+0

esta es la mejor respuesta – danny117

+3

Este código debe entrar en 'onViewCreated' en el Fragmento – Flexicoder

13

Dependiendo de lo que quiere darse cuenta de que puede utilizar los métodos expuestos para menús de contexto:

primer registro de la clase View que obtiene larga presiona (dentro de su clase Fragmento):

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    registerForContextMenu(this.getListView()); 
} 

Luego implemente estos dos métodos, para crear un menú contextual y hacer lo que quiera cuando se hace clic en un elemento del menú:

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    MenuInflater inflater = this.getActivity().getMenuInflater(); 
    inflater.inflate(R.menu.my_context_menu, menu); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
    switch (item.getItemId()) { 

     case R.id.add: // <-- your custom menu item id here 
      // do something here 
      return true; 

     default: 
      return super.onContextItemSelected(item); 
    } 
} 
+4

¡Agradable! Solo quiero agregar que no se puede combinar registerForContextMenu con setOnItemLongClickListener. Puede ser obvio para algunos, pero no para mí. También es bueno saber que la identificación de la fila se puede recuperar de info.id – Toydor

+0

Hoy primera vez he aumentado todas las respuestas porque esto era lo que iba a hacer con el evento longclick. – danny117

Cuestiones relacionadas