2011-09-14 21 views
6

Tengo una actividad ListFragment.Cómo anular LongPress en ListFragment?

Quiero crear un método para onItemClickedLongPress, de modo que cuando el usuario hace esto. aparece un menú. Estoy familiarizado con la creación del menú.

Entonces, si alguien quisiera, dame más instrucciones sobre cómo establecer Anular el LongPress en una actividad ListFragment?

Respuesta

8

editar: esta muestra muestra cómo mostrar algo distinto del menú del sistema fx. QuickAction de https://github.com/lorensiuswlt/NewQuickAction

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    //....... 
    registerForContextMenu(getListView()); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    AdapterView.AdapterContextMenuInfo amenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo; 
    Object item = getListAdapter().getItem(amenuInfo.position); 
    //item could be Cursor/String/YourObject it depends on Adapter 
    //show popup fx. QuickAction from https://github.com/lorensiuswlt/NewQuickAction 
    QuickAction qa = new QuickAction(getActivity()); 
    qa.setAnimStyle(QuickAction.ANIM_AUTO); 
    qa.show(amenuInfo.targetView); 
} 

EDIT: Este ansewer no es bueno ... por qué lo hice tal método extraño? porque intelisense Eclipse no propmt "bueno" para setOnLongClickListenerListView (ya ListView tiene por lo menos 2 métodos setOnLongClickListener ... uno de View y segunda clase) de AdapterView ... la manera más fácil es dejar que su ListFragment aplicar AdapterView.OnItemLongClickListener y luego en onViewCreated complemento código getListView().setOnLongClickListener(this);

+0

menuInfo es nulo? Qué hacer ahora ? – aProgrammer

5

Por "pulsación larga", creo que se está refiriendo al menú contextual. Para una ListFragment, todo lo que tiene que hacer es registrarse en el menú contextual:

@Override 
public void onActivityCreated(Bundle icicle) {  
    registerForContextMenu(getListView()); 
} 

Una vez hecho esto, el ListFragment deben llamar onCreateContextMenu() y onContextItemSelected() cuando detecta una pulsación larga.

0

Modificado respuesta Erich Douglass más .. por alguna razón mi propia aplicación se estrellaría hasta que he modificado mi código y se coloca en el registro onViewCreated de la siguiente manera:

@Override 
public void onViewCreated (View view, Bundle savedInstanceState) { 
    registerForContextMenu(getListView()); 
} 
0
getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
     // Show your popout menu here. 
    } 
}); 
Cuestiones relacionadas